TableKit
TableKit 是一个超轻量级且功能强大的通用库,允许您以声明式类型安全的方式构建复杂的表格视图。它隐藏了幕后 UITableViewDataSource
和 UITableViewDelegate
方法的复杂性,因此您的代码看起来干净、易于阅读和维护。
功能
- 类型安全的通用单元格
- 函数式编程风格友好
- 轻松地将模型或视图模型映射到单元格
- 自动单元格注册*
- 正确处理支持多行标签的自动布局单元格
- 可链式的单元格动作(选择/取消选择等)
- 支持从代码、xib 或 storyboard 创建单元格
- 支持不同的单元格高度计算策略
- 支持纵向和横向方向
- 无需子类化
- 可扩展性
开始使用
包含演示 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
要使用CocoaPods将TableKit集成到您的Xcode项目中,请在您的Podfile
中指定它
pod 'TableKit'
Carthage
在您的Cartfile
中添加一行github "maxsokolov/tablekit"
。
手册
克隆仓库并将《源》文件夹中的文件拖拽到您的 Xcode 项目中。
需求
- iOS 8.0
- Xcode 9.0
变更日志
请注意查看 变更信息。
许可
TableKit 在 MIT 许可证下可用。详细信息见 LICENSE 文件。