iOS开发——UI篇Swift篇&玩转UItableView(四)自定义&封装
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(四)自定义&封装的更多相关文章
- iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...
- iOS开发——新特性Swift篇&Swift 2.0 异常处理
Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS开发——网络编程Swift篇&Alamofire详解
Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...
- ios开发——实用技术总结Swift篇&swift常用开发技术总结
swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...
- iOS开发——网络编程Swift篇&(八)SwiftyJSON详解
SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...
- iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画
CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...
- iOS开发零基础--Swift篇 元组
元组的介绍 元组是Swift中特有的,OC中并没有相关类型 它是什么呢? 它是一种数据结构,在数学中应用广泛 类似于数组或者字典 可以用于定义一组数据 组成元组类型的数据可以称为“元素” 元组的定义 ...
- iOS开发零基础--Swift篇 循环
循环的介绍 在开发中经常会需要循环 常见的循环有:for/while/do while. 这里我们只介绍for/while,因为for/while最常见 for循环的写法 最常规写法 // 传统写法 ...
随机推荐
- 理解KMP
KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n),KMP匹配算法,可以证明它的时间复杂度为O(m+n).. 一.简单匹配算法 先来看一个 ...
- C++学习之路--类的构建以及数据转换存储
注意理解下面的代码,数据的处理与转换. 头文件: #ifndef STACK_H #define STACK_H class Stack { struct Link { void* data; Lin ...
- python堡垒机
堡垒机 windows下安装python3的paramiko模块后一些报错总结: error: Unable to find vcvarsall.bat [官网对此问题的描述] : https://d ...
- CentOS6.5修改mysql数据文件路径
1.停止mysql服务 service mysql stop 2.移动数据文件位置(保留原文件权限) cp -a /var/lib/mysql /mysqldata 3.修改/et ...
- poj2528(线段树+离散化)Mayor's posters
2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...
- dom 拖拽回放
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- RAD XE10 Seattle
RAD Studio 10 Seattle RAD XE10 Seattle RAD 10 Seattle c++builder 10 Seattle Delphi 10 Seattle http:/ ...
- 60分钟内从零起步驾驭Hive实战学习笔记
本博文的主要内容是: 1. Hive本质解析 2. Hive安装实战 3. 使用Hive操作搜索引擎数据实战 SparkSQL前身是Shark,Shark强烈依赖于Hive.Spark原来没有做SQL ...
- index structure
1. wordlist 0) 0, 1byte 1) token-id(delta), 8byte 2) doclist-offset(delta), 8byte 3) doc_count, 4byt ...
- Wisdombud.CommonTool及其应用
@(编程) 1. 用法 student类 using System.ComponentModel; namespace WindowsFormsApplication1 { public class ...