UItableView自定义&封装

一:Model

 class AppsModel: NSObject {

     //定义模型的三个属性
     var imageName:String!  //图片名称
     var appName:String!     //应用名称
     var appDescription:String!      //应用描述

     //自定义初始化方法
     init(imageName image_Name:String , app_Name:String , app_Description:String) {
         self.imageName=image_Name
         self.appName=app_Name
         self.appDescription=app_Description
     }

     // MARK: - NSCoding
     func encodeWithCoder(_encoder: NSCoder)
     {
         _encoder.encodeObject(self.imageName, forKey: "M_imageName")
         _encoder.encodeObject(self.appName, forKey: "M_appName")
         _encoder.encodeObject(self.appDescription, forKey: "M_appDescription")
     }

     init(coder decoder: NSCoder)
     {
 //        imageName = decoder.decodeObjectForKey("M_imageName") as String
 //        appName = decoder.decodeObjectForKey("M_appName") as String
 //        appDescription = decoder.decodeObjectForKey("M_appDescription") as String

         //2015年5月2号修改
         imageName = decoder.decodeObjectForKey("M_imageName") as! String
         appName = decoder.decodeObjectForKey("M_appName") as! String
         appDescription = decoder.decodeObjectForKey("M_appDescription") as! String
     }

 }

二:View

 class MyTableViewCell: UITableViewCell {

     var iconImageView:UIImageView!   //图片
     var appNameLabel:UILabel!        //标题
     var decLabel:UILabel!            //描述

     //赋值方法 - 显示cell内容方法
     func showAppInfoWithModel(model:AppsModel)
     {
         //获取model中得图片
         iconImageView.image = UIImage(named: model.imageName)

         //获取model中app名称
         appNameLabel.text = model.appName

         //获取model中app描述
         decLabel.text = model.appDescription
     }

     override init(style: UITableViewCellStyle, reuseIdentifier: String?)
     {
         super.init(style: style, reuseIdentifier: reuseIdentifier)

         //创建iconImageView
         iconImageView = UIImageView(frame: CGRectMake(, , , ))
         self.addSubview(iconImageView)

         //创建appNameLabel
         appNameLabel = UILabel(frame: CGRectMake(, , , ))
         appNameLabel.font = UIFont.systemFontOfSize()
         self.addSubview(appNameLabel)

         //创建decLabel
         decLabel = UILabel(frame: CGRectMake(, , , ))
         decLabel.font = UIFont.systemFontOfSize()
         decLabel.numberOfLines =
         decLabel.textColor = UIColor.lightGrayColor()
         self.addSubview(decLabel)

     }

     required init(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }

     override func awakeFromNib() {
         super.awakeFromNib()
         // Initialization code
     }

     override func setSelected(selected: Bool, animated: Bool) {
         super.setSelected(selected, animated: animated)

         // Configure the view for the selected state
     }

 }

三:Controller

 class UITableViewControllerCustom: UIViewController, UITableViewDataSource, UITableViewDelegate  {

     var titleString:String!

     @IBOutlet var titleLabel:UILabel!
     @IBOutlet var listTableView : UITableView!

     //定义数组
     var items:[AppsModel]!

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

         //定义三个模型对象
         var model1:AppsModel = AppsModel(imageName: "appIcon1.png", app_Name: "Football Maze", app_Description: "足球迷宫,迷宫的新玩法,益智虚拟迷宫游戏。快来挑战你的空间想象,足球迷宫带你到一个不同的世界… 迷宫大家都在玩,你还在等什么。")
         var model2:AppsModel = AppsModel(imageName: "appIcon2.png", app_Name: "租房点评", app_Description: "租房被骗?现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!")
         var model3:AppsModel = AppsModel(imageName: "appIcon3.png", app_Name: "iJump", app_Description: "摇动手机,松鼠就可以运动啦,越跳越高,注意会有虫子咬坏跳板哦,祝你玩得开心")
         var model4:AppsModel = AppsModel(imageName: "appIcon4.png", app_Name: "哪里逃", app_Description: "哪里逃 是一款躲避类游戏,拖动美女图片,躲避,追来的帅锅,帅锅人数越来越多,不要被追到哦。")

         //修改数组值
         items = [model1,model2,model3,model4]

         //TabelView刷新
         listTableView.reloadData()

         // Do any additional setup after loading the view.
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     //MARK: - UITableViewDelegate
     //tableView数据源:返回行数
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
     {
         return items.count
     }

     //tableView 数据源:每一行高度
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
     {

     }

     //tableView数据源:每一行内容
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
     {
         //Cell标示符,代表一系列
         // OC:使用static,  swift:使用let
         let cellIdentifier: String = "cellIdentifier"

         //通过cellIdentifier标示符取没有使用的Cell
         //有可能不存在,所以使用:optional
         var cell: MyTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? MyTableViewCell

         //如果cell取到是空
         if cell == nil { // no value

             //创建新的MyTableViewCell实例
             //cell样式:UITableViewCellStyle.Default
             //cell标示符:cellIdentifier
             cell = MyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)

             //设置字体
 //            cell!.textLabel.font = UIFont.systemFontOfSize(14)
             //2015年4月10号修改
             cell!.textLabel?.font = UIFont.systemFontOfSize()

             //设置选中cell样式
             cell!.selectionStyle = .Gray;

             //设置cell后面箭头样式
             cell!.accessoryType = .DisclosureIndicator;
         }

         var cellModel:AppsModel = self.items[indexPath.row]

         //通过自定义方法给cell赋值
         cell?.showAppInfoWithModel(cellModel)

         return cell!;
     }

     //tableView代理:点击一行
     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
     {
         //释放选中效果
          tableView.deselectRowAtIndexPath(indexPath, animated: true)

         var urlString:String! = "https://itunes.apple.com/us/app/fruit-storm/id859713088?l=zh&ls=1&mt=8"

         {
             urlString = "https://itunes.apple.com/us/app/football-maze/id879720177?l=zh&ls=1&mt=8"
         }
         {
             urlString = "https://itunes.apple.com/us/app/zu-fang-dian-ping/id893902071?l=zh&ls=1&mt=8"
         }
         {
             urlString = "https://itunes.apple.com/us/app/ijump/id877475648?l=zh&ls=1&mt=8"
         }
         {
             urlString = "https://itunes.apple.com/us/app/na-li-tao/id880016522?l=zh&ls=1&mt=8"
         }

         UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
     }
 }
 

iOS开发——UI篇Swift篇&玩转UItableView(四)自定义&封装的更多相关文章

  1. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

  2. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发——网络编程Swift篇&Alamofire详解

    Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...

  5. ios开发——实用技术总结Swift篇&swift常用开发技术总结

    swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...

  6. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  7. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  8. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  9. iOS开发零基础--Swift篇 元组

    元组的介绍 元组是Swift中特有的,OC中并没有相关类型 它是什么呢? 它是一种数据结构,在数学中应用广泛 类似于数组或者字典 可以用于定义一组数据 组成元组类型的数据可以称为“元素” 元组的定义 ...

  10. iOS开发零基础--Swift篇 循环

    循环的介绍 在开发中经常会需要循环 常见的循环有:for/while/do while. 这里我们只介绍for/while,因为for/while最常见 for循环的写法 最常规写法 // 传统写法 ...

随机推荐

  1. bzoj 2038 [2009国家集训队]小Z的袜子(hose)(莫队算法)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2038 [题意] 给定一个有颜色的序列,回答若干个询问:区间内任选两个颜色相同的概率. ...

  2. Eclipse使用技巧总结

    Eclipse设置工作空间的字符编码: 打开eclipse开发界面,依次点击Window->Preferences->General->Workspace 修改Text file e ...

  3. Hadoop入门简介

    一.Hadoop简介 1.1.Hadoop主要进行分布式存储和分布式计算 1.1-1.HDFS:分布式文件系统 1.1-2.MapReduce:并行计算框架 1.2.Hadoop用来做什么? 搭建大型 ...

  4. 20151227感知机(perceptron)

    1 感知机 1.1 感知机定义 感知机是一个二分类的线性分类模型,其生成一个分离超平面将实例的特征向量,输出为+1,-1.导入基于误分类的损失函数,利用梯度下降法对损失函数极小化,从而求得此超平面,该 ...

  5. C常用字符字符串处理函数

    1.strtok();字符串分片函数

  6. 制作炫酷banner js插件,revolution

    这是一款非常强大的内容切换插件,它基于jQuery,它充分响应,支持移动设备,支持手机触摸,键盘翻页:它内置幻灯.视频播放计时器,它拥有各种模式:自定义,自动响应,全屏:它有多种动画效果.3d效果.. ...

  7. ASA与PIX的区别

    很多年来,Cisco PIX一直都是Cisco确定的防火墙.但是在2005年5月,Cisco推出了一个新的产品——适应性安全产品(ASA,Adaptive Security Appliance).不过 ...

  8. 关于datatable的一些操作以及使用adapter对数据的操作

    private void updateToolStripMenuItem_Click(object sender, EventArgs e) {//将数据更新回数据库 //获取源数据 DataTabl ...

  9. MSSQL手札四 MSSQL的函数

    和oracle一样,sql也可以自己定义函数 一个返回值,引用DEMO如下: 编写一个函数,该函数,可以通过输入借书时间来判断是否到期,当借阅时间大于30天,返回已经过期:否则返回还未到期. CREA ...

  10. 编译安装-PHP

    一.编译配置选项2 配置帮助表:2 安装目录:2 交叉编译选项:2 特征选项:3 SAPI模块设置:3 普通参数设置:4 扩展参数:4 PEAR相关选项:9 ZEND相关选项:9 TSRM线程安全资源 ...