TableBuilder - 更方便快捷构建TableView页面

简介

更方便快捷构建TableView页面

优点:

  • 更少代码
  • 声明式
  • 灵活度高
  • 基于 UITableViewDelegate & UITableViewDataSource
  • 已经处理好Cell复用
  • 方便重写和扩展

示例

要运行示例项目,请克隆仓库,并首先从示例目录运行pod install

要求

Swift 5+

安装

TableBuilder可以通过CocoaPods获得。安装
只需将下面这行添加到你的Podfile中:

1
pod 'TableBuilder'

使用

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import TableBuilder

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var tableProxy: TableProxy!

override func viewDidLoad() {
super.viewDidLoad()
reloadTable()
}

var show: Bool = true

func reloadTable() {
tableProxy = TableProxy(tableView) { [weak self] in
guard let self = self else { return nil }
return TableBuilder {
for _ in 0..<3 {
TableBuilder.Section(
headerHeight: 50,
headerReuse: .anyClass(UITableViewHeaderFooterView.self, { tableView, section, reusableView in
reusableView.contentView.backgroundColor = .red
}, { tableView, reusableView, indexPath in
/// headerWillDisplay
reusableView.layoutIfNeeded()
reusableView.contentView.cutRectCorner([.topRight, .bottomLeft], cornerRadius: 25)
})) {
TableBuilder.Row(
cellHeight: 50,
cellType: TableViewCell1.self, reuseType: .nib
) { tableView, indexPath, cell in

} didSelectRowAtIndexPath: { tableView, indexPath, cell in
print("CellType1的单独的点击事件")
}
TableBuilder.Row(
cellHeight: 50,
cellType: TableViewCell2.self, reuseType: .anyClass
) { tableView, indexPath, cell in
cell.contentView.backgroundColor = .green
cell.textLabel?.text = "\(indexPath.row)"
} didSelectRowAtIndexPath: { tableView, indexPath, cell in
print("=====Cell类型2的单独的点击事件")
} willDisplay: { tableView, cell, indexPath in
cell.contentView.cutRectCorner([.topLeft, .bottomRight], cornerRadius: 25)
}
let count = 10
for _ in 0..<count {
TableBuilder.Row(
cellHeight: 30,
autoCellHeight: false,
cellType: UITableViewCell.self,
reuseType: .anyClass
) { tableView, indexPath, cell in
cell.contentView.backgroundColor = .blue
cell.textLabel?.text = "\(indexPath.row)"
}
}
if self.show {
TableBuilder.Row(
cellHeight: 50,
cellType: UITableViewCell.self
) { tableView, indexPath, cell in
cell.contentView.backgroundColor = .purple
cell.textLabel?.text = "\(indexPath.row)"
}
TableBuilder.Row(
cellHeight: 50,
cellType: UITableViewCell.self
) { tableView, indexPath, cell in
cell.contentView.backgroundColor = .purple
cell.textLabel?.text = "\(indexPath.row)"
}
}
else {
TableBuilder.Row(
cellHeight: 90,
cellType: UITableViewCell.self
){ tableView, indexPath, cell in
cell.contentView.backgroundColor = .yellow
cell.textLabel?.text = "\(indexPath.row)"
}
}
}
}
}
}
tableProxy.didSelectRowAtIndexPath = { tableView, indexPath in
print("clicked: \(indexPath.section) - \(indexPath.row)")
}
}
}

开源许可

TableBuilder在MIT许可下可用。查看许可证文件以获取更多信息。


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

TableBuilder - 更方便快捷构建TableView页面
https://cba023.github.io/posts/4e3de635/
作者
陈波
发布于
2023年8月3日
许可协议