swift3使用协议和泛型简化UITableView的使用(续)多种类型的cell注册
来源:互联网 发布:python能用来做什么 编辑:程序博客网 时间:2024/06/07 01:01
1、协议
//// ReusableCell.swift// reusecell//// Created by targetcloud on 2017/3/26.// Copyright © 2017年 targetcloud. All rights reserved.//import UIKitprotocol ReusableCell : class{ static var reuseableIdentifier :String {get} static var nib : UINib? {get}}extension ReusableCell where Self : UITableViewCell{ static var reuseableIdentifier :String{ //return "\(self)" //两种实现方式都可以 print(" --- \(self) \(String(describing: self)) --- ") return String(describing: self) } static var nib : UINib? { return nil }}extension UITableView{ func registerCell<T : UITableViewCell >(_ cell : T.Type) where T : ReusableCell{ if let nib = T.nib{ register(nib, forCellReuseIdentifier: T.reuseableIdentifier) }else{ register(cell, forCellReuseIdentifier: T.reuseableIdentifier) } } func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) -> T where T : ReusableCell{ return dequeueReusableCell(withIdentifier: T.reuseableIdentifier, for: indexPath) as! T }}
//// TGNib2Cell.swift// reusecell//// Created by targetcloud on 2017/3/26.// Copyright © 2017年 targetcloud. All rights reserved.//import UIKitclass TGNib2Cell: UITableViewCell,ReusableCell { static var nib : UINib?{ return UINib(nibName: "TGNib2Cell", bundle: nil) } }
3、使用
//// ViewController.swift// reusecell//// Created by targetcloud on 2017/3/26.// Copyright © 2017年 targetcloud. All rights reserved.//import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let tv = UITableView(frame: view.bounds) tv.dataSource = self tv.registerCell(TGCell.self) tv.registerCell(TGNibCell.self) tv.registerCell(TGNib2Cell.self) view.addSubview(tv) }}extension ViewController :UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row % 2 == 1 { let cell = (tableView.dequeReusableCell(indexPath: indexPath) as TGNibCell) cell.nibTestLabel?.text = " --- ReusableCellFromNib \(indexPath.row) --- " return cell } if indexPath.row % 3 == 2 { let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- " return cell } let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGNib2Cell return cell }}
多种cell类型同时注册后的代码执行过程
先是系统注册三种类型的cell,会打印三次类型,分别为
--- TGCell TGCell ---
--- TGNibCell TGNibCell ---
--- TGNib2Cell TGNib2Cell ---
然后是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
当它调用dequeReusableCell来到了
func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) -> T where T : ReusableCell
当它需要确定何种类型的cell时,来到了
static var reuseableIdentifier :String
得到了类型后再原路返回cell
前三次的代码执行又会返回以下结果
--- TGNib2Cell TGNib2Cell ---
--- TGNibCell TGNibCell ---
--- TGCell TGCell ---
0 0
- swift3使用协议和泛型简化UITableView的使用(续)多种类型的cell注册
- swift3使用协议和泛型简化UITableView的使用
- UITableView cell 的动态使用
- 13 自定义cell、cell自适应高度、 多种类型的cell混合使用、 UITableViewController、model类
- UITableView使用自定义cell的例子
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- iOS7中UITableView中cell的使用
- 使用UITableView+FDTemplateLayoutCell计算cell的高度
- swift3.0 使用第三方加载cell的网络图片
- 如何修改一次代码就可以完成多种类型 cell 的 UITableView 增删修改顺序?
- UIday1101:UITableView 3 自定义cell 多种cell混合使用 cell自适应高度
- 复用多种类型的cell
- Unity3D说明文档翻译-Graphics
- 动态化加载已安装的apk
- JQuery中serialize()用法实例分析
- Unity3D说明文档翻译-Physics
- elasticsearch集群安装与使用
- swift3使用协议和泛型简化UITableView的使用(续)多种类型的cell注册
- 使用Notepad++替换多个文件的相同内容
- Unity3D说明文档翻译-Scripting
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
- Unity3D说明文档翻译-Audio
- Unity3D说明文档翻译-Animation
- 使用操作对象的简单例子(63)
- CSS选择器优先级
- Unity3D说明文档翻译-Navigation and Pathfinding