UItableView自定义&封装

一:Model

  1. class AppsModel: NSObject {
  2.  
  3. //定义模型的三个属性
  4. var imageName:String! //图片名称
  5. var appName:String! //应用名称
  6. var appDescription:String! //应用描述
  7.  
  8. //自定义初始化方法
  9. init(imageName image_Name:String , app_Name:String , app_Description:String) {
  10. self.imageName=image_Name
  11. self.appName=app_Name
  12. self.appDescription=app_Description
  13. }
  14.  
  15. // MARK: - NSCoding
  16. func encodeWithCoder(_encoder: NSCoder)
  17. {
  18. _encoder.encodeObject(self.imageName, forKey: "M_imageName")
  19. _encoder.encodeObject(self.appName, forKey: "M_appName")
  20. _encoder.encodeObject(self.appDescription, forKey: "M_appDescription")
  21. }
  22.  
  23. init(coder decoder: NSCoder)
  24. {
  25. // imageName = decoder.decodeObjectForKey("M_imageName") as String
  26. // appName = decoder.decodeObjectForKey("M_appName") as String
  27. // appDescription = decoder.decodeObjectForKey("M_appDescription") as String
  28.  
  29. //2015年5月2号修改
  30. imageName = decoder.decodeObjectForKey("M_imageName") as! String
  31. appName = decoder.decodeObjectForKey("M_appName") as! String
  32. appDescription = decoder.decodeObjectForKey("M_appDescription") as! String
  33. }
  34.  
  35. }

二:View

  1. class MyTableViewCell: UITableViewCell {
  2.  
  3. var iconImageView:UIImageView! //图片
  4. var appNameLabel:UILabel! //标题
  5. var decLabel:UILabel! //描述
  6.  
  7. //赋值方法 - 显示cell内容方法
  8. func showAppInfoWithModel(model:AppsModel)
  9. {
  10. //获取model中得图片
  11. iconImageView.image = UIImage(named: model.imageName)
  12.  
  13. //获取model中app名称
  14. appNameLabel.text = model.appName
  15.  
  16. //获取model中app描述
  17. decLabel.text = model.appDescription
  18. }
  19.  
  20. override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  21. {
  22. super.init(style: style, reuseIdentifier: reuseIdentifier)
  23.  
  24. //创建iconImageView
  25. iconImageView = UIImageView(frame: CGRectMake(, , , ))
  26. self.addSubview(iconImageView)
  27.  
  28. //创建appNameLabel
  29. appNameLabel = UILabel(frame: CGRectMake(, , , ))
  30. appNameLabel.font = UIFont.systemFontOfSize()
  31. self.addSubview(appNameLabel)
  32.  
  33. //创建decLabel
  34. decLabel = UILabel(frame: CGRectMake(, , , ))
  35. decLabel.font = UIFont.systemFontOfSize()
  36. decLabel.numberOfLines =
  37. decLabel.textColor = UIColor.lightGrayColor()
  38. self.addSubview(decLabel)
  39.  
  40. }
  41.  
  42. required init(coder aDecoder: NSCoder) {
  43. fatalError("init(coder:) has not been implemented")
  44. }
  45.  
  46. override func awakeFromNib() {
  47. super.awakeFromNib()
  48. // Initialization code
  49. }
  50.  
  51. override func setSelected(selected: Bool, animated: Bool) {
  52. super.setSelected(selected, animated: animated)
  53.  
  54. // Configure the view for the selected state
  55. }
  56.  
  57. }

三:Controller

  1. class UITableViewControllerCustom: UIViewController, UITableViewDataSource, UITableViewDelegate {
  2.  
  3. var titleString:String!
  4.  
  5. @IBOutlet var titleLabel:UILabel!
  6. @IBOutlet var listTableView : UITableView!
  7.  
  8. //定义数组
  9. var items:[AppsModel]!
  10.  
  11. //返回按钮事件
  12. @IBAction func backButtonClick()
  13. {
  14. self.navigationController?.popViewControllerAnimated(true)
  15. }
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. titleLabel.text = titleString
  21.  
  22. //定义三个模型对象
  23. var model1:AppsModel = AppsModel(imageName: "appIcon1.png", app_Name: "Football Maze", app_Description: "足球迷宫,迷宫的新玩法,益智虚拟迷宫游戏。快来挑战你的空间想象,足球迷宫带你到一个不同的世界… 迷宫大家都在玩,你还在等什么。")
  24. var model2:AppsModel = AppsModel(imageName: "appIcon2.png", app_Name: "租房点评", app_Description: "租房被骗?现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!")
  25. var model3:AppsModel = AppsModel(imageName: "appIcon3.png", app_Name: "iJump", app_Description: "摇动手机,松鼠就可以运动啦,越跳越高,注意会有虫子咬坏跳板哦,祝你玩得开心")
  26. var model4:AppsModel = AppsModel(imageName: "appIcon4.png", app_Name: "哪里逃", app_Description: "哪里逃 是一款躲避类游戏,拖动美女图片,躲避,追来的帅锅,帅锅人数越来越多,不要被追到哦。")
  27.  
  28. //修改数组值
  29. items = [model1,model2,model3,model4]
  30.  
  31. //TabelView刷新
  32. listTableView.reloadData()
  33.  
  34. // Do any additional setup after loading the view.
  35. }
  36.  
  37. override func didReceiveMemoryWarning() {
  38. super.didReceiveMemoryWarning()
  39. // Dispose of any resources that can be recreated.
  40. }
  41.  
  42. /*
  43. // MARK: - Navigation
  44.  
  45. // In a storyboard-based application, you will often want to do a little preparation before navigation
  46. override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
  47. // Get the new view controller using segue.destinationViewController.
  48. // Pass the selected object to the new view controller.
  49. }
  50. */
  51.  
  52. //MARK: - UITableViewDelegate
  53. //tableView数据源:返回行数
  54. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  55. {
  56. return items.count
  57. }
  58.  
  59. //tableView 数据源:每一行高度
  60. func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
  61. {
  62.  
  63. }
  64.  
  65. //tableView数据源:每一行内容
  66. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  67. {
  68. //Cell标示符,代表一系列
  69. // OC:使用static, swift:使用let
  70. let cellIdentifier: String = "cellIdentifier"
  71.  
  72. //通过cellIdentifier标示符取没有使用的Cell
  73. //有可能不存在,所以使用:optional
  74. var cell: MyTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? MyTableViewCell
  75.  
  76. //如果cell取到是空
  77. if cell == nil { // no value
  78.  
  79. //创建新的MyTableViewCell实例
  80. //cell样式:UITableViewCellStyle.Default
  81. //cell标示符:cellIdentifier
  82. cell = MyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
  83.  
  84. //设置字体
  85. // cell!.textLabel.font = UIFont.systemFontOfSize(14)
  86. //2015年4月10号修改
  87. cell!.textLabel?.font = UIFont.systemFontOfSize()
  88.  
  89. //设置选中cell样式
  90. cell!.selectionStyle = .Gray;
  91.  
  92. //设置cell后面箭头样式
  93. cell!.accessoryType = .DisclosureIndicator;
  94. }
  95.  
  96. var cellModel:AppsModel = self.items[indexPath.row]
  97.  
  98. //通过自定义方法给cell赋值
  99. cell?.showAppInfoWithModel(cellModel)
  100.  
  101. return cell!;
  102. }
  103.  
  104. //tableView代理:点击一行
  105. func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  106. {
  107. //释放选中效果
  108. tableView.deselectRowAtIndexPath(indexPath, animated: true)
  109.  
  110. var urlString:String! = "https://itunes.apple.com/us/app/fruit-storm/id859713088?l=zh&ls=1&mt=8"
  111.  
  112. {
  113. urlString = "https://itunes.apple.com/us/app/football-maze/id879720177?l=zh&ls=1&mt=8"
  114. }
  115. {
  116. urlString = "https://itunes.apple.com/us/app/zu-fang-dian-ping/id893902071?l=zh&ls=1&mt=8"
  117. }
  118. {
  119. urlString = "https://itunes.apple.com/us/app/ijump/id877475648?l=zh&ls=1&mt=8"
  120. }
  121. {
  122. urlString = "https://itunes.apple.com/us/app/na-li-tao/id880016522?l=zh&ls=1&mt=8"
  123. }
  124.  
  125. UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
  126. }
  127. }
 

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.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名 下面的脚本适合你: /*********************** ...

  2. [转] Web前端优化之 CSS篇

    原文链接: http://lunax.info/archives/3097.html Web 前端优化最佳实践第四部分面向 CSS.目前共计有 6 条实践规则.另请参见 Mozilla 开发者中心的文 ...

  3. Arduino+RFID RC522 +继电器

    博客园的第一篇博文就献给Arduino了.不知道能不能坚持自己喜欢的并且记录下来. 起码是个好的开始. 想实现一卡通代替钥匙开启电动车. 简单的原理,通过RC522模块读取一卡通的序列号,在程序中进行 ...

  4. hadoop 伪分布模式的配置

    转自 http://blog.csdn.net/zhaogezhuoyuezhao/article/details/7328313 centos系统自带ssh,版本为openssh4.3 免密码ssh ...

  5. the behavior of the UICollectionViewFlowLayout is not defined because:

    the behavior of the UICollectionViewFlowLayout is not defined because:    the item height must be le ...

  6. 我的第一个CUDA程序

    最近在学习CUDA框架,折腾了一个多月终于把CUDA安装完毕,现在终于跑通了自己的一个CUDA的Hello world程序,值得欣喜~ 首先,关于CUDA的初始化,代码和解释如下,这部分主要参考GXW ...

  7. [原创]Devexpress XtraReports 系列 2 创建表格报表

    昨天发表了Devexpress XtraReports系列开篇,今天我们继续. 今天的主题是创建表格报表. 首先我们来看看最后实现的效果.Demo最后附上. 接下来开始讲解如何一步一步做出这个报表: ...

  8. Jsp页面设计易忘点

    文本标签: <b>文本加粗 <i>斜体 <u>下划线 <sub>作为下标 <sup>作为上标 样式: italic;斜体 text-deco ...

  9. msyql数据库主从架构

    在Web应用系统中,数据库性能是导致系统性能瓶颈最主要的原因之一.尤其是在大规模系统中,数据库集群已经成为必备的配置之一.集群的好处主要有:查询负载.数据库复制备份等. MySQL数据库支持数据库的主 ...

  10. 手把手教你玩转SOCKET模型之重叠I/O篇(上)

    “身为一个初学者,时常能体味到初学者入门的艰辛,所以总是想抽空作点什么来尽我所能的帮助那些需要帮助的人.我也希望大家能把自己的所学和他人一起分享,不要去鄙视别人索取时的贪婪,因为最应该被鄙视的是不肯付 ...