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

2025-05-29 0 100

前言

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
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只要遵循它,就不用每次都要写一大堆的代理方法,下面就是我写的一个代理类的使用

示例代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
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

总结

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS中TableView如何统一数据源代理详解 https://www.kuaiidc.com/88973.html

相关文章

发表评论
暂无评论