史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView
来源:互联网 发布:同步带设计软件下载 编辑:程序博客网 时间:2024/05/19 20:00
史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView
开发环境
Mac OS 10.12+ / Xcode 8+ / Swift 3+
支持环境
iOS 8+, iPhone & iPad
项目获取
此处代码由Swift3.1展示,推荐使用Swift,项目已经上传至github中SimplifiedCellHeaderFooter(https://github.com/cba023/SimplifiedCellHeaderFooter),若要使用,请导入文件到您的项目。
功能展示
使用说明
导入项目
手动导入
如图所示,将“SimplifiedCellHeaderFooter”文件夹拖入要用到该框架的工程中,在Swift项目中,可直接对其进行使用。
函数调用
- cell数据源调用
// cell 数据源 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.cell(aClass: DemoCell1.self) as! DemoCell1 cell.lbl1.text = "\(indexPath.section) ==> \(indexPath.row)" return cell }
- header数据源 或 footer数据源
// header 数据源 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return tableView.headerFooter(aClass: DemoViewOfHeader1.self, closure: { (viewIn) in let v = viewIn as! DemoViewOfHeader1 v.lbl1.text = "section: \(section)" v.lbl2.text = "as: Header" v.backgroundColor = .orange }) }
实现原理
函数封装
cell数据源函数封装
// 基于直接加载XIB复用Cell的函数 func cell(aClass: UITableViewCell.Type?) -> UITableViewCell? { let className = "\(String(describing: aClass!))" var cell = self.dequeueReusableCell(withIdentifier: className) if cell == nil { cell = (Bundle.main.loadNibNamed(className, owner: nil, options: nil)?.first as! UITableViewCell) } return cell }
header或footer数据源函数封装
// MARK: - 复用header或footer视图 func headerFooter(aClass: UIView.Type?, closure:(UIView)->()) -> BaseTableViewHeaderFooterView { let hf: BaseTableViewHeaderFooterView? let className = "\(String(describing: aClass!))" var headerFooter = self.dequeueReusableHeaderFooterView(withIdentifier: className) // 新创建 if headerFooter == nil { headerFooter = BaseTableViewHeaderFooterView(reuseIdentifier: className) hf = headerFooter as? BaseTableViewHeaderFooterView hf?.viewReuse = UIView.loadViewFromBundle1st(view: className) hf?.viewReuse.frame = (headerFooter?.bounds)! headerFooter?.addSubview((hf?.viewReuse)!) } else { hf = headerFooter as? BaseTableViewHeaderFooterView } closure((hf?.viewReuse)!) return hf! }
XIB第一个视图的加载
// MARK: - 加载XIB第一个视图extension UIView { static func loadViewFromBundle1st(view nibName:String) -> UIView { return ((Bundle.main.loadNibNamed(nibName, owner: nil, options: nil))?.first) as! UIView }}
XIB创建与关联
- 创建cell和XIB,创建后要检查XIB与复用的cell(header或footer)是否关联
通过简单的封装UITableView可以在非常简洁的情况下调用cell,header,footer等视图了,免去了每次在数据源函数判断视图是否为空或在UITableView初始化时注册的麻烦,没有读懂?请原谅我写得不太优美,欢迎去github下载https://github.com/cba023/SimplifiedCellHeaderFooter。
亲,学会了吧?赶快去嗨皮吧!
致读者
该项目已经上传至github中SimplifiedCellHeaderFooter(https://github.com/cba023/SimplifiedCellHeaderFooter),可以在那里直接star 或者fork 该项目,它可能会长期的帮助您高效地进行程序开发,当然也欢迎留言,有不足或者错误的地方可以随时指正,您的指导和建议是我前行路上新的动力!
- 史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView
- swift UItableView和自定义uitableViewCell
- ISO 用 Xib 制作UITableView 和 UITableViewCell
- Customise UITableViewHeaderFooterView through XIB
- ios UItableView,UITableViewHeaderFooterView分组
- Swift 最佳实践规范
- 代码实现 UITableView与UITableViewCell
- 代码实现 UITableView与UITableViewCell
- 代码实现 UITableView与UITableViewCell
- 代码实现 UITableView与UITableViewCell
- Swift中UITableView的用法(自定义UITableView和UITableViewCell)
- UITableViewCell使用自动布局的“最佳实践”
- Swift-UITableView的实现
- Swift编程(一):UITableView及自定义Cell的Xib
- UITableView&UITableViewCell
- ios – 使用UINib加载xib文件实现UITableViewCell
- ios使用UINib加载xib文件实现UITableViewCell
- ios – 使用UINib加载xib文件实现UITableViewCell
- python os.stat() 和 stat模块详解
- node.js学习笔记2——querystring (查询字符串,参数处理)
- Git高频命令(长期更新)
- C语言指针学习笔记
- 数据结构编程笔记十七:第六章 树和二叉树 赫夫曼树的实现
- 史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView
- requests库入门-5-带参数的请求类型
- 微服务的设计原则
- JVM笔记
- 冒泡排序法
- 狄克斯特拉算法(入门)
- Java 割圆术球π
- Linux(64位)下OpenBabel 2.4.1、python2.7和Ipython实战(三)
- MATLAB 视角调整