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. 备份数据库SQL Server 2008下实测

    下面的存储过程适用: 1.一次想备份多个数据库. 2.只需要一步操作,在有存储过程的条件下. 3.可以根据自己的需要修改存储过程. /*----------------------------- De ...

  2. gVIM 简洁配置 in Windows

    原文链接:http://www.errdev.com/post/2/ 捣鼓了一段时间的VIM,神器终归是神器,果然编码效率提升了许多,当然还需要很多插件来配合.自己装插件很麻烦,还要有Vundle这个 ...

  3. Chapter 7 Windows下pycaffe的使用之draw_net.py

    Chapter 6 中完成了在Windows下,对pycaffe的编译,如果编译存在问题,请参考:http://www.cnblogs.com/xiaopanlyu/p/6158902.html 本文 ...

  4. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  5. hdu 2819 Swap

    Swap http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...

  6. emWin(ucGui)数值显示例程 -【worldsing笔记】

    本例程下载:2.emWin5.26(ucGui)VS2008数字显示.zip   在emWin显示文本字符还是容易,我们也可以使用字符串和标准 C 库的函数来显示数值.然而,有时候这会是件困难的事.通 ...

  7. QT输入输出(一) 之 QDataStream 测试

    QT提供了两个高级别的流类---QDataStream和QTextStream,可以从任意的输入输出设备读取或写入数据. QDataStream用于读写二进制数据,它的优点是:在读写数据的时候已经严格 ...

  8. OC:属性、点语法、KVC

    //属性的属性 属性定义在一个 .h文件里,在这个.h文件里可以定义实例变量(就是这个类的特征),也可以通过   @protery(属性约束关键字) 属性名字类型 属性名 来定义一些属性,在prope ...

  9. Apache实现动态虚拟主机

    经常在开发中为Apache web server添加虚拟主机  方便多个项目的 同时运营,但是每次增加新的项目时都得重新配置增加VirtualHost:虚拟主机    部分,时间久了VirtualHo ...

  10. JavaScript 跨域:谈谈跨域之 JSONP

    在 Web 开发中,后台开发人员应该会通常遇到这个问题:跨域,而使用 JSONP 就是其中解决办法之一,当然,还有其它解决方法,比如:window.name.window.postMessage.CO ...