Index
示例
要运行示例项目,请首先将仓库克隆,并在Example目录中运行pod install
需求
安装
Index可通过CocoaPods获取。要安装它,只需在Podfile中添加以下行
pod 'Index'
定义
1. IndexProtocol
IndexProtocol 是 derive 类中使用的主要协议。它包含用于创建索引元素数组的方法。
- .value: Index?
- .indexPath: IndexPath
- .isSelected: Bool { get }
- .select(_ isSelected: Bool)
- .init()
2. 行
Row 类具有 Index 泛型类型,以符合 IndexProtocol。所有在父协议中指定的方法的主要思想是从自身创建一个新实例。例如,要更新行列表,需要重置行数组。
以下是一些示例
class TestViewController: UIViewController {
var cars: [Row<Car>] = []
func didDownload(_ array: [Car]) {
self.cars = array.enumerated().map {
Row($0.element, indexPath: .init(row: $0.offset, section: 0))
}
}
}
Row 类帮助收集单元格或表格单元格重新加载其状态以及现在值,如 indexPath 或是否选中。
class TestViewCell: UITableViewCell {
func reloadState(_ index: Row<Car>) {
guard !index.isEmpty else {
return
}
self.titleLabel.text = index.item.name
self.priceLabel.text = index.item.price.asPrice
if index.isSelected {
self.selectedLayout()
} else {
self.notSelectedLayout()
}
}
}
3. 部分
部分类和 Row.Index 类有两个泛型类型 X 和 Y。这只是一个模板,用于为 SectionRow 类做准备。
针对 Section 实现的这些方法包括:
- .init(_ section: X, items: [Y], section index: Int = 0)
- .asSectionRows: [SectionRow]
4. SectionRow
SectionRow 是 Row 的包装器,因为它允许我们访问部分属性而无需调用额外的方法。
它有:
- .value: Section?
- .row: Row
- .init(_ section: Section, row: Row)
- .indexPath: IndexPath
- .isSelected: Bool
- .isFirst: Bool
- .isLast: Bool
- .select(_ isSelected: Bool)
5. SectionDataSource
这有助于将具有 .items: [T] { get } 的类转换为 Section 类。如果您有一个 [SectionDataSource] 的数组,您可以通过调用 .asSectionRow 或 .asSection 属性来获得一个列表的数据,这些数据可以用于我们的表视图中。
6. 标识符
此类实现了 identifier 属性获取器和符合 Hashable,允许您使用这些元素的唯一数组。
额外子规范扩展
1. UIContainer
来自UIContainer,这个扩展帮助将索引数组转换为UIContainer视图数组。
class TestViewController: UIViewController {
weak var carsSV: UIStackView!
func reloadCars(_ rows: [Row<Car>]) {
self.carsSV.arrangedSubviews.forEach {
$0.removeFromSubview()
}
CarView.Container.asCells(rows, in: self)
.forEach { container, row in
container.view.reloadState(row)
carsSV.addArrangedSubview(container)
}
}
}
2. IndexBindable
从UMUtils实现ViewModel的,IndexBindable允许使用RxSwift和RxCocoa库创建更反应式的单元格。
这包括以下两个类:
- RowBindable
- SectionRowBindable
以下是一些示例
class CarView: View, RowModel, ContainerCellDelegate {
typealias Item = Car
override func prepare() {
super.prepare()
self.titleLabel.font = UIFont(name: "RobotoFont-regular", size: 16.0)
self.priceLabel.font = UIFont(name: "RobotoFont-bold", size: 18.0)
self.viewModel = RowModel()
}
override func reloadViews(_ index: Row<Car>) {
guard !index.isEmpty else {
return
}
self.titleLabel.text = index.item.name
self.priceLabel.text = index.item.price.asPrice
if index.isSelected {
self.selectedLayout()
} else {
self.notSelectedLayout()
}
}
}
extension TestView {
class Cell: ContainerTableViewCell<TestView> {
override func containerDidLoad() {
super.containerDidLoad()
self.selectionStyle = .none
}
}
}
在我们的视图控制器中我们需要的:
extension TestViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.cars.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestViewCellIdentifier", for: indexPath) as! TestView.Cell
cell.prepareContainer(inside: self)
cell.view.configure(row)
return cell
}
}
作者
brennobemoura,[email protected]
许可证
Index遵守MIT许可证。有关更多信息,请参阅LICENSE文件。