【发布时间】:2018-06-27 05:02:28
【问题描述】:
我不知道如何处理您的数据源。 我观看了有关 RxSwift 和 RxCocoa 的课程,并意识到对于复杂的表,您需要使用数据源。找到了一个库 RxDataSourced 但我们不允许使用它。决定编写自己的数据源,但什么也没发生。不介意的话可以举个写dataSourse的例子,网上一无所获。
class RXSpecificationsDataSource: NSObject, RxTableViewDataSourceType, UITableViewDataSource {
typealias Element = MaterialEntityRootGroupProperty
var _sectionModels: [MaterialEntityRootGroupProperty] = []
public typealias ConfigureCell = (RXSpecificationsDataSource, UITableView, IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, observedEvent: Event<RXSpecificationsDataSource.Element>) { }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard _sectionModels.count > section else { return 0 }
return _sectionModels[section].properties.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
precondition(indexPath.item < _sectionModels[indexPath.section].properties.count) return ConfigureCell(self, tableView, indexPath, self[indexPath])
}
func numberOfSections(in tableView: UITableView) -> Int {
return _sectionModels.count
}
}
final class MaterialDetailSpecificationsView: UserInterface {
private var specificationTableView: UITableView!
private let disposeBag = DisposeBag()
func setupView(items: Variable<[MaterialEntityRootGroupProperty]>) {
setNabigationParms()
drawTableViewSpecifications()
let dataSource = RXSpecificationsDataSource ()
dataSource.configureCell = { (dataSource, tv, indexPath, element) in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = "\(element) @ row \(indexPath.row)"
return cell
}
items.asObservable()
.bind(to: specificationTableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
【问题讨论】:
标签: ios swift datasource rx-swift rx-cocoa