TableKit 是一个超级轻量级但功能强大的通用库,它允许你以声明式类型安全的方式构建复杂的表格视图。它在幕后隐藏了 UITableViewDataSource
和 UITableViewDelegate
方法的复杂性,使你的代码看起来更简洁、易读且易于维护。
包含一个示例应用,演示了 TableKit 的功能。
创建你的行
import TableKit
let row1 = TableRow<StringTableViewCell>(item: "1")
let row2 = TableRow<IntTableViewCell>(item: 2)
let row3 = TableRow<UserTableViewCell>(item: User(name: "John Doe", rating: 5))
将行放入分区
let section = TableSection(rows: [row1, row2, row3])
并设置你的表格
let tableDirector = TableDirector(tableView: tableView)
tableDirector += section
完成。你的表格已准备就绪。你的单元格必须遵循 ConfigurableCell
协议
class StringTableViewCell: UITableViewCell, ConfigurableCell {
func configure(with string: String) {
textLabel?.text = string
}
}
class UserTableViewCell: UITableViewCell, ConfigurableCell {
static var estimatedHeight: CGFloat? {
return 100
}
// is not required to be implemented
// by default reuse id is equal to cell's class name
static var reuseIdentifier: String {
return "my id"
}
func configure(with user: User) {
textLabel?.text = user.name
detailTextLabel?.text = "Rating: \(user.rating)"
}
}
你可以根据需要拥有任意数量的行和分区。
拥有一些与你的单元格相关的操作会很好
let action = TableRowAction<StringTableViewCell>(.click) { (options) in
// you could access any useful information that relates to the action
// options.cell - StringTableViewCell?
// options.item - String
// options.indexPath - IndexPath
// options.userInfo - [AnyHashable: Any]?
}
let row = TableRow<StringTableViewCell>(item: "some", actions: [action])
或者,使用优雅的链式方法
let row = TableRow<StringTableViewCell>(item: "some")
.on(.click) { (options) in
}
.on(.shouldHighlight) { (options) -> Bool in
return false
}
你可以在这里找到所有可用的操作。
你可以定义你自己的操作
struct MyActions {
static let ButtonClicked = "ButtonClicked"
}
class MyTableViewCell: UITableViewCell, ConfigurableCell {
@IBAction func myButtonClicked(sender: UIButton) {
TableCellAction(key: MyActions.ButtonClicked, sender: self).invoke()
}
}
并相应地处理它们
let myAction = TableRowAction<MyTableViewCell>(.custom(MyActions.ButtonClicked)) { (options) in
}
也可以使用具有相同类型的多个操作
let click1 = TableRowAction<StringTableViewCell>(.click) { (options) in }
click1.id = "click1" // optional
let click2 = TableRowAction<StringTableViewCell>(.click) { (options) in }
click2.id = "click2" // optional
let row = TableRow<StringTableViewCell>(item: "some", actions: [click1, click2])
如果你想以某种方式分离你的逻辑,这可能很有用。操作将按照它们附加的顺序被调用。
如果你定义了具有相同类型的多个操作,并且这些操作也返回值,则只有最后一个返回值将用于表格视图。
你也可以通过 id 删除任何操作
row.removeAction(forActionId: "action_id")
默认情况下,TableKit 依赖于自适应大小单元格。在这种情况下,你必须为你的单元格提供一个估计高度
class StringTableViewCell: UITableViewCell, ConfigurableCell {
// ...
static var estimatedHeight: CGFloat? {
return 255
}
}
这对于大多数情况来说已经足够了。但你可能对此不满意。因此,你可以使用原型单元格来计算单元格高度。要启用此功能,只需使用此属性
let tableDirector = TableDirector(tableView: tableView, shouldUsePrototypeCellHeightCalculation: true)
它在幕后为你完成所有原型单元格的繁琐工作,因此你无需担心任何事情,只需关注你的单元格配置即可
class ImageTableViewCell: UITableViewCell, ConfigurableCell {
func configure(with url: NSURL) {
loadImageAsync(url: url, imageView: imageView)
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
multilineLabel.preferredMaxLayoutWidth = multilineLabel.bounds.size.width
}
}
你还必须为你的所有多行标签额外设置 preferredMaxLayoutWidth
。
处理表格视图从未如此简单。
let users = /* some users array */
let click = TableRowAction<UserTableViewCell>(.click) {
}
let rows = users.filter({ $0.state == .active }).map({ TableRow<UserTableViewCell>(item: $0.name, actions: [click]) })
tableDirector += rows
完成,你的表格已准备就绪。
TableKit 可以自动在表格视图中注册你的单元格。如果你的可重用单元格 id 与单元格的 xib 名称匹配
MyTableViewCell.swift
MyTableViewCell.xib
你也可以关闭此行为
let tableDirector = TableDirector(tableView: tableView, shouldUseAutomaticCellRegistration: false)
并手动注册你的单元格。
要使用 CocoaPods 将 TableKit 集成到你的 Xcode 项目中,请在你的 Podfile
中指定它
pod 'TableKit'
将行 github "maxsokolov/tablekit"
添加到你的 Cartfile
中。
克隆 repo 并将文件从 Sources
文件夹拖到你的 Xcode 项目中。
关注更新。
TableKit 在 MIT 许可证下可用。有关详细信息,请参阅 LICENSE。