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

2020-01-21 06:19:53丽君

前言

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)
  }
 }