Tablet是一个非常轻量但功能强大的通用地库,它在Swift环境中处理UITableView的dataSource和delegate方法。Tablet的目标是提供一个创建复杂表格视图的最简单方式。使用Tablet,在处理不同部分中的不同cell时,您无需编写混乱的switch
或if
语句。
这几乎是构建一个部分中几组细胞所需的所有内容
let builder = TableRowBuilder<String, MyTableViewCell>(items: ["1", "2", "3", "4", "5"])
Tablet依赖于自适应表格视图单元格,尊重cell的可重用性特性,并且以性能为出发点。您无需担心任何问题,只需创建您的cell,设置自动布局约束,并享受其中的乐趣。参见使用部分以了解更多信息。
假设您想将cell配置逻辑放入cell本身。假设您想将视图模型传递给cell。您可以使用TableRowBuilder
轻松完成此操作。您的cell应遵循ConfigurableCell
协议,如下面的示例中所示
import Tablet
class MyTableViewCell : UITableViewCell, ConfigurableCell {
typealias T = User
// this method is not required to be implemented if your cell's id equals to class name
static func reusableIdentifier() -> String {
return "reusable_id"
}
static func estimatedHeight() -> Float {
return 255
}
func configure(item: T) { // item is user here
textLabel?.text = item.username
detailTextLabel?.text = item.isActive ? "Active" : "Inactive"
}
}
一旦实现了该协议,就可以简单地使用TableRowBuilder
构建单元格
import Tablet
let rowBuilder = TableRowBuilder<User, MyTableViewCell>()
rowBuilder += users
director = TableDirector(tableView: tableView)
tableDirector += TableSectionBuilder(rows: [rowBuilder])
您可能想设置一个非常基础的表格视图,没有任何自定义单元格。在这种情况下,只需使用TableBaseRowBuilder
。
import Tablet
let rowBuilder = TableBaseRowBuilder<User, UITableViewCell>(items: [user1, user2, user3], id: "reusable_id")
.action(.configure) { (data) in
data.cell?.textLabel?.text = data.item.username
data.cell?.detailTextLabel?.text = data.item.isActive ? "Active" : "Inactive"
}
let sectionBuilder = TableSectionBuilder(headerTitle: "Users", footerTitle: nil, rows: [rowBuilder])
director = TableDirector(tableView: tableView)
director += sectionBuilder
Tablet提供了一种链式方法来处理单元格操作
import Tablet
let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: [user1, user2, user3], id: "reusable_id")
.action(.configure) { (data) in
}
.action(.click) { (data) in
}
.valueAction(.shouldHighlight) { (data) in
return false
}
import Tablet
struct MyCellActions {
static let ButtonClicked = "ButtonClicked"
}
class MyTableViewCell : UITableViewCell {
@IBAction func buttonClicked(sender: UIButton) {
Action(key: MyCellActions.ButtonClicked, sender: self, userInfo: nil).invoke()
}
}
并使用行构建器接收这些操作
import Tablet
let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
.action(.click) { (data) in
}
.action(.willDisplay) { (data) in
}
.action(MyCellActions.ButtonClicked) { (data) in
}
如果你发现Tablet没有提供所需的功能,例如你需要UITableViewDelegate的didEndDisplayingCell
方法,而这些方法没有现成的,只需为TableDirector
提供一个扩展即可
import Tablet
struct MyTableActions {
static let DidEndDisplayingCell = "DidEndDisplayingCell"
}
extension TableDirector {
public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
invoke(action: .custom(MyTableActions.DidEndDisplayingCell), cell: cell, indexPath: indexPath)
}
}
使用行构建器捕获你的操作
let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
.action(MyTableActions.DidEndDisplayingCell) { (data) -> Void in
}
你还可以调用返回值的操作。
Tablet遵循MIT许可协议。查看LICENSE细节。