iOS中TableView如何统一数据源代理详解

来自:互联网
时间:2020-05-26
阅读:

前言

TableView 是 iOS 应用程序中非常通用的组件,几乎每一个界面都有一个TableView,而我们许多的代码都和TableView有关系,比如数据展示、更新TableView,一些响应选择事件等,而这些大多都会通过其代理函数来实现,所以在VC中我们通常需要实现大量TableView的代理函数,如下面这样

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
 return 12.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
 return 0.01
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
 return 44.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
 return nil
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
 return nil
}
func numberOfSections(in tableView: UITableView) -> Int {
 return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 tableView.deselectRow(at: indexPath, animated: true)
}

如果上面的代码在每个VC中都实现一次,不仅写了很多的重复的代码,还增加了VC的复杂度,所以我在想能不能有一个统一的代理类,我们的TableView只要遵循它,就不用每次都要写一大堆的代理方法,下面就是我写的一个代理类的使用

示例代码

 private var delegate = CCDataSource()

 lazy private var tableView: UITableView = {
  let table = UITableView(frame: self.view.bounds, style: .grouped)
  // 1.注册cell
  table.register(Custom1Cell.self, forCellReuseIdentifier: "cell1")
  table.register(Custom2Cell.self, forCellReuseIdentifier: "cell2")
  // 2.代理
  table.delegate = self.delegate
  table.dataSource = self.delegate
  return table
 }()

 override func viewDidLoad() {
  super.viewDidLoad()
  self.view.addSubview(tableView)
  self.setupTableView()
  self.loadData()
 }

 private func loadData() {
  // 3.网络请求数据源,并赋值
  delegate.datas = [[Model1(),Model1(),Model1()],[Model2(),Model2(),Model2(),Model2()]]
  // 4.刷新视图
  tableView.reloadData()
 }

 private func setupTableView() {
  // 在这里实现TableView的代理
  delegate.identifier { (indexPath) -> (String) in
   // 5.确定cell的类型
   return indexPath.section == 0 ? "cell1" : "cell2"
  }.headerHeight { (section) -> (CGFloat) in
   // 6.头部高度
   return 12.0
  }.footerHeight { (section) -> (CGFloat) in
   // 7.尾部高度
   return 0.01
  }.rowHeight{ (indexPath, data) -> (CGFloat) in
   // 8.行高
   if let model = data as? Model1 {
    return model.height()
   }
   if let model = data as? Model2 {
    return model.height()
   }
   return 44.0
  }.setContentCell { (cell, data) -> (Void) in
   // 9.配置数据源
   if let item = cell as? Custom1Cell, let model = data as? Model1 {
    item.textLabel?.text = "Custom1Cell" + model.description
   }
   if let item = cell as? Custom2Cell, let model = data as? Model2 {
    item.textLabel?.text = "Custom2Cell" + model.description
   }
  }.selected {[weak self] (indexPath, data) -> (Void) in
   // 10.点击事件(这里[weak self]需要防止循环引用)
   self?.navigationController?.pushViewController(ViewController(), animated: true)
  }
 }
  1. 注册cell:这一步很重要,这个代理类只支持这种方式加载cell,你在该界面有几种cell,就需要注册几个cell类 代理: 将代理实例赋值给tableView的代理,这里我将dataSource和delegate统一为delegate了,并且如果有多个TableView,我们还可以创建多个代理实例与其一一对应 网络请求:这里是做网络请求地方,并且将请求后的数据保存在代理类中 刷新视图 确定cell的类型:cell是通过它注册identifier来创建的,所以根据indexPath来返回相应的cell注册的identifier即可 头部高度:header的高度,可以是定值,也可以根据section来动态返回 尾部高度:footer的高度,可以是定值,也可以根据section来动态返回 行高:这里的行高可以通过data来获取,这样利于做高度缓存,也可以通过indexPath来动态返回 配置数据源:这里可以获取的已经初始化号的cell和其对应的数据源,我们只需要将其赋值给cell即可 点击事件

上面这些步骤也不是固定的,这里有链式编程的思想,有些属性可以不设置则会取默认值,当然也可以重复设置,不过此时后面的会覆盖前面的

通过上面的方法,我们只需要创建一个CCDataSource实例,就可以在一个方法中将所有的TableView代理实现,而且在第5步时,我们就将cell与data对应起来了,后面会减少很多复杂的if else判断,这不仅减少了代码量,同时也使实现逻辑更加清晰

Demo地址:https://github.com/cdcyd/CCDataSource 

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

返回顶部
顶部