ExtendedTableViewController
使用方法
显示数据
创建扩展表格视图控制器第一步是子类化。所有的显示和编辑部分和行所需的功能都已集成在 EXTableViewController 中。因此,只需创建一个新的 EXTableViewController 子类。
class ViewController: EXTableViewController {}第二步是创建所需的表格视图单元格。创建一个新的表格视图单元格,从 ConfigurableTableViewCell 继承。实现所需的协议。
class IntTableViewCell: ConfigurableTableViewCell {
// 1
class var reuseIdentifier: String { return "IntTableViewCell" }
class var bundle: Bundle { return Bundle(for: IntTableViewCell.self) }
class var nib: UINib { return UINib(nibName: "IntTableViewCell", bundle: bundle) }
// 2
func configure(for value: Int) {
textLabel?.text = "Value"
detailTextLabel?.text = String(value)
}
}- 这允许单元格轻松出列。确保重用标识符与 storyboard 匹配。
- 这个方法将用值来配置单元格。所有配置都应在这里完成。
下一步是创建行。这是表示表格视图特定行的对象。
struct IntRow: Row {
// 1
typealias DataType = Int
typealias CellType = IntTableViewCell
// 2
var data: Int
// 3
init(data: Int) {
self.data = data
}
}- 类型别名是该协议的关联类型,基本上将数据类型映射到单元格类型。现在,每次使用此行时,IntTableViewCell 将自动使用变量
data配置。 请注意,DataType必须与CellType.DataType相同。 - 这是行中实际存储的数据。例如,这可能是客户数据、字符串、姓名等。它将在单元格中显示。
- 此初始化器几乎总是会相同:只需设置
data存储属性。
最后一步只是用数据填充表格视图,这非常简单。在 ViewController 类中,简单地重写 generateSections 方法。在方法体中,生成表格视图的部分和行。
override func generateSections() -> [Section] {
// 1
var sections: [Section] = []
// 2
var section1 = Section()
section1.headerTitle = "Section 1"
// 3
section1.appendRow(IntRow(data: 1))
section1.appendRow(StringRow(data: "Awesome"))
section1.appendRow(IntRow(data: 22))
// 4
sections.append(section1)
var section2 = Section()
section2.headerTitle = "Section 2"
section2.appendRow(IntRow(data: 13))
// 5
section2.appendRow(StringRow(data: "So cool"))
section2.appendRow(StringRow(data: "Heck yeah"))
sections.append(section2)
return sections
}- 这是将要返回的分区数组。所有生成的分区都应添加到此数组中。
- 这是创建新分区的方式。之后,您可以添加行、更改标题等。
- 简单地初始化新行(
IntRow(data: 1))并将其追加到分区。就这么简单。 - 请确保将分区追加到分区数组。
- 注意您可以追加任何类型的行。这里,我添加了另一种类型的行,
StringRow,没有任何额外开销。
现在运行项目,您就有了一个完全功能的扩展表格视图控制器。太好了!
响应用户交互
传统响应用户交互的方法仍然有效。然而,它已经被优化,也可以是声明性的。例如,您不必使用tableView(_:didSelectRowAt:),只需用someRow.onDidSelect = ...告诉行在被点击时该做什么。但是,这仅在符合EXTableViewController中respoder协议的行上有效。这是因为ExtendedTableViewController在每次可观测事件期间都会检查respoder。
UITableViewDelegate方法 |
响应等价 |
|---|---|
tableView(_:willDisplay:forRowAt:) |
WillDisplayResponder.onWillDisplay |
tableView(_:willSelectRowAt:) |
WillSelectResponder.onWillSelect |
tableView(_:didSelectRowAt:) |
DidSelectResponder.onDidSelect |
tableView(_:willDeselectRowAt:) |
WillDeselectResponder.onWillDeselect |
tableView(_:didDeselectRowAt:) |
DidDeselectResponder.onDidDeselect |
可选地,您可以遵守FullResponder以获取访问所有方法的能力。
struct IntRow: Row, DidSelectResponder {
...
var onDidSelect: ((IndexPath) -> ())?
}let row = IntRow(data: 500)
row.onDidSelect = { _ in
print("Just tapped 500!")
}额外的单元格配置
有时在单元格的初始配置完成之后,可能需要对其进行额外的定制。可以通过使您的行遵守ConfigurableRow而不是Row来实现这一点。
struct IntRow: ConfigurableRow {
...
var configuration: ((IntTableViewCell) -> ())?
}现在,在配置过程中,单元格将自动传递给configuration。通过在分区生成期间设置configuration,可以定制单元格
override func generateSections() -> [Section] {
...
let row = IntRow(data: 13)
row.configuration = { cell in
cell.textLabel?.textColor = .red
}
...
}虽然此功能可用,但建议仅在确实需要时才这样做。在表格视图中显示数据应处理在单元格中。
标题和页脚视图
在EXTableViewController中显示标题和页脚视图也非常简单,有几种方法可以实现。以下列表按优先顺序排列了选项。
Section.headerView或Section.footerView- 用于显示为标题/页脚的单个视图。Section.reusableHeaderViewClass或Section.reusableFooterViewClass- 可重复使用的标题/页脚视图类。EXTableViewController.defaultHeaderViewClass或EXTableViewController.defaultFooterViewClass- 表观中默认的标题/页脚,如果分区未提供。