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
将 github "maxsokolov/tablekit"
这行添加到您的 Cartfile
中。
手动
克隆仓库并将 Sources
文件夹中的文件拖入您的 Xcode 项目的文件中。
需求
- iOS 8.0
- Xcode 9.0
变更日志
请注意查看 变更。
许可证
TableKit 基于 MIT 许可证提供。有关详细信息,请参阅许可证文件。