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. “内部类” 大总结(Java)

    (本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...

  2. MySQL安装(图文详解)

    下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下!打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”,出现如下界面 my ...

  3. ps做gif love教程(转)

    先看看效果吧: 这是在写部教程的时候,看到一个由方格组成的心.于是试着用PS做成了动画,然后加入了LOVE四个字母,看起来还可以.但是,有些复杂.复杂倒不是技术上的复杂,是做起来复杂. 来试试吧. 1 ...

  4. AI钻石天鹅风格

    第1步:描绘轮廓 你需要对你的设计有个总体的概念.利用照片和钢笔工具(P)描出轮廓.把填充颜色设为无,描边颜色设为黑色,1pt 粗细.这将作为你完成剩下设计的指导.编组 (Ctrl+G)你的线条并在图 ...

  5. hadoop2.2伪分布安装加2.2源码编译

    配置linux基本环境: --> java.ip.hostname.hosts.iptables.chkconfig.ssh环境配置 hadoop2.2安装在linux64位机器上,需要对源码进 ...

  6. Module compiled with Swift 3.0 cannot be imported in Swift 3.0.1

    Cartfile:github "SwiftyJSON/SwiftyJSON"got error:Module compiled with Swift 3.0 cannot be ...

  7. ZOJ-3201 Tree of Tree 树形DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3201 题意:给一颗树,每个节点有一个权值,求节点数为n的最大权子 ...

  8. dom cookie记录用户名

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Instagram的技术探索2(转)

    原文:http://www.cnblogs.com/xiekeli/archive/2012/05/28/2520770.html 前一篇翻译了Instagram blog上的一篇文章<What ...

  10. 【多线程】JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...