【问题标题】:RxSwift, How to draw tableView without using RxDataSourced?RxSwift,如何在不使用 RxDataSourced 的情况下绘制 tableView?
【发布时间】: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


    【解决方案1】:

    您不需要 RxDataSources 来将流绑定到表。 RxCocoa 中已经内置了 UITableView 和 UICollectionView 的绑定,你可以在这里查看更多信息:

    RxDataSources repo 中实际上有一个没有RxDataSources 的示例;-)

    let data = Observable<[String]>.just(["first element", "second element", "third element"])
    
    data.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { index, model, cell in
      cell.textLabel?.text = model
    }
    .disposed(by: disposeBag)
    

    https://github.com/RxSwiftCommunity/RxDataSources#why

    【讨论】:

    • 是的,有一个例子,但很简单。我需要实现类并补充 tableView 方法。例如:func numberOfSections(in: UITableView) 等。我想找到在没有 RxDataSources 的情况下完成你的硬数据源的人。您展示了一个很好的示例,但是您无法控制部分的数量
    • 我认为如果你想要更复杂的数据源,最好在这里使用 RxDataSources。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-10-11
    相关资源
    最近更新 更多