CombineDataSources 提供了自定义的 Combine 订阅器,这些订阅器充当表格和集合视图控制器,并将元素集合流绑定到带有单元格的表格或集合部分。
1.1 绑定元素的纯列表
1.2 绑定 Section 模型列表
1.2 自定义列表控制器
1.3 分批加载列表
2.2 Cocoapods
该仓库在 Example 子文件夹中包含一个演示应用,演示了在实践中使用 CombineDataSources 的不同方法。
var data = PassthroughSubject<[Person], Never>()
data
.bind(subscriber: tableView.rowsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
cell.nameLabel.text = model.name
}))
.store(in: &subscriptions)
分别为集合视图
data
.bind(subscriber: collectionView.itemsSubscriber(cellIdentifier: "Cell", cellType: PersonCollectionCell.self, cellConfig: { cell, indexPath, model in
cell.nameLabel.text = model.name
cell.imageURL = URL(string: "https://api.adorable.io/avatars/100/\(model.name)")!
}))
.store(in: &subscriptions)
var data = PassthroughSubject<[Section<Person>], Never>()
data
.bind(subscriber: tableView.sectionsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
cell.nameLabel.text = model.name
}))
.store(in: &subscriptions)
var data = PassthroughSubject<[[Person]], Never>()
let controller = TableViewItemsController<[[Person]]>(cellIdentifier: "Cell", cellType: PersonCell.self) { cell, indexPath, person in
cell.nameLabel.text = person.name
}
controller.animated = false
// More custom controller configuration ...
data
.bind(subscriber: tableView.sectionsSubscriber(controller))
.store(in: &subscriptions)
列表视图的常见模式是以“批次”或“页面”加载非常长的元素列表。(区别在于页面意味着有序的,等长的批次。)
CombineDataSources 包含一个数据源,允许您轻松实现名为 BatchesDataSource
的分批列表模式,以及一个表格视图控制器 TableViewBatchesController
,它通过上述数据源包装了分批加载项目并管理您的 UI。
如果您想实现自己的自定义逻辑,可以直接使用数据源类型
let input = BatchesInput(
reload: resetSubject.eraseToAnyPublisher(),
loadNext: loadNextSubject.eraseToAnyPublisher()
)
let dataSource = BatchesDataSource<String>(
items: ["Initial Element"],
input: input,
initialToken: nil,
loadItemsWithToken: { token in
return MockAPI.requestBatchCustomToken(token)
})
dataSource
通过两个输入控制
input.reload
(重新加载第一批)和
loadNext
(加载下一批)
数据源有四个输出
output.$items
是当前元素列表,
output.$isLoading
是否正在获取一批元素,
output.$isCompleted
数据源是否已获取所有可用元素,以及
output.$error
这是一个 Error?
元素流,加载闭包产生的错误将在其中冒泡。
如果您想使用提供的控制器,代码也相当简单。您可以像这样使用标准表格视图项目控制器和 TableViewBatchesController
let itemsController = TableViewItemsController<[[String]]>(cellIdentifier: "Cell", cellType: UITableViewCell.self, cellConfig: { cell, indexPath, text in
cell.textLabel!.text = "\(indexPath.row+1). \(text)"
})
let tableController = TableViewBatchesController<String>(
tableView: tableView,
itemsController: itemsController,
initialToken: nil,
loadItemsWithToken: { nextToken in
MockAPI.requestBatch(token: nextToken)
}
)
tableController
将设置表格视图数据源,获取项目,并使用适当的动画显示单元格。
将以下依赖项添加到您的 Package.swift 文件中
.package(url: "https://github.com/combineopensource/CombineDataSources, from: "0.2")
将以下依赖项添加到您的 Podfile 中
pod 'CombineDataSources'
CombineOpenSource 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。
CombineOpenSource Slack 频道: https://combineopensource.slack.com。
由 Marin Todorov 为 CombineOpenSource 创建。
📚 您可以通过查看我们的 Combine 书籍来支持我: combinebook.com。
灵感来自 RxDataSources 和 RxRealmDataSources。