簡介
更方便快捷構(gòu)建TableView頁面
優(yōu)點(diǎn):
- 更少代碼
- 聲明式
- 靈活度高
- 基于 UITableViewDelegate & UITableViewDataSource
- 已經(jīng)處理好Cell復(fù)用
- 方便重寫和擴(kuò)展
示例
要運(yùn)行示例項目砾淌,請克隆倉庫成榜,并首先從示例目錄運(yùn)行pod install
烘贴。
要求
Swift 5+
安裝
TableBuilder可以通過CocoaPods獲得纺裁。安裝
只需將下面這行添加到你的Podfile中:
pod 'TableBuilder'
使用
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() {
// 綁定構(gòu)造器
tableProxy = tableView.bd.build(TableBuilder {
for _ in 0..<3 {
// 創(chuàng)建TableViewSection
TableSectionBuilder {
// 創(chuàng)建TableViewRow
TableRowBuilder(
cellHeight: 50,
cellType: TableViewCell1.self, reuseType: .nib)
{ tableView, indexPath, cell in
} didSelectRowAtIndexPath: { tableView, indexPath, cell in
print("CellType1的單獨(dú)的點(diǎn)擊事件")
}
TableRowBuilder(
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的單獨(dú)的點(diǎn)擊事件")
}
let count = 10
for _ in 0..<count {
TableRowBuilder(
cellHeight: 30,
autoCellHeight: false,
cellType: UITableViewCell.self,
reuseType: .anyClass)
{ tableView, indexPath, cell in
// cell定制
cell.contentView.backgroundColor = .blue
cell.textLabel?.text = "\(indexPath.row)"
}
}
if show {
TableRowBuilder(
cellHeight: 50,
cellType: UITableViewCell.self)
{ tableView, indexPath, cell in
cell.contentView.backgroundColor = .purple
cell.textLabel?.text = "\(indexPath.row)"
}
TableRowBuilder(
cellHeight: 50,
cellType: UITableViewCell.self)
{ tableView, indexPath, cell in
cell.contentView.backgroundColor = .purple
cell.textLabel?.text = "\(indexPath.row)"
}
}
else {
TableRowBuilder(
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)")
}
tableView.reloadData()
}
}
開源許可
TableBuilder在MIT許可下可用。查看許可證文件以獲取更多信息告组。