当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS中TableView如何统一数据源代理详解

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

2019年07月24日  | 移动技术网移动技术  | 我要评论
前言 tableview 是 ios 应用程序中非常通用的组件,几乎每一个界面都有一个tableview,而我们许多的代码都和tableview有关系,比如数据展示、更新

前言

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类
  2. 代理: 将代理实例赋值给tableview的代理,这里我将datasource和delegate统一为delegate了,并且如果有多个tableview,我们还可以创建多个代理实例与其一一对应
  3. 网络请求:这里是做网络请求地方,并且将请求后的数据保存在代理类中
  4. 刷新视图
  5. 确定cell的类型:cell是通过它注册identifier来创建的,所以根据indexpath来返回相应的cell注册的identifier即可
  6. 头部高度:header的高度,可以是定值,也可以根据section来动态返回
  7. 尾部高度:footer的高度,可以是定值,也可以根据section来动态返回
  8. 行高:这里的行高可以通过data来获取,这样利于做高度缓存,也可以通过indexpath来动态返回
  9. 配置数据源:这里可以获取的已经初始化号的cell和其对应的数据源,我们只需要将其赋值给cell即可
  10. 点击事件

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

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

demo地址:https://github.com/cdcyd/ccdatasource ()

总结

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网