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. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  2. Bluebird-Collections

    Promise实例方法和Promise类核心静态方法用于处理promise或者混合型(mixed)promise和值的集合. 所有的集合实例方法都等效于Promise对象中的静态方法,例如:someP ...

  3. adb device出现error:unknown host service

    最近在windows下使用adb device时,提示“ADB server didn't ACK” & “error: unknown host service”错误. 网上搜索了下,是说a ...

  4. 【boost】MFC dll中使用boost thread的问题

    项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...

  5. 【转】webgame前台开发总结--虽然是10年的文章,但是也有参考价值

    一.webgame整个游戏流程: 1.预加载(打开游戏页面后,显示进度条,主要加载前期的登陆和创建角色资源,创建角色资源的加载可以放到进入创建角色界面的时候加载,因为玩家除了第一次进入游戏,其他时间基 ...

  6. js混淆工具

    1\  http://www.jasob.com 2\ http://developer.yahoo.com/yui/compressor

  7. accordion data-options iconCls

    <div id="aa" class="easyui-accordion" style="width:500px;height:300px;&q ...

  8. 第三百三十六天 how can I 坚持

    家里断网了,忘交网费了,连的手机网络,也挺好,吃完饭就可以睡觉了. 不知道怎的,昨天和家人聊天,一提对象的事就很容易着急生气,然后就会后悔..哎,这脾气得改. 确实不知道自己的另一半是啥样,想象不出来 ...

  9. 【转】log4j详解及简易搭建

    原文链接:http://www.cnblogs.com/mailingfeng/archive/2011/07/28/2119937.html log4j是一个非常强大的log记录软件. 首先当然是得 ...

  10. tomcat的 JNDI 配置

    tomcat的conf/server.xml 配置 尽量用简单版 <Context path="/cas" docBase="D:\YC\zqV7\cas\WebR ...