首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在RxSwiftDataSource中绑定多个数据和单元

在RxSwiftDataSource中绑定多个数据和单元
EN

Stack Overflow用户
提问于 2022-04-06 18:43:28
回答 1查看 230关注 0票数 1

我试图在同一个tableView中显示多个单元格和它们自己的数据模型。我为此添加了段控制,并删除了所有表视图当前数据,并删除了当前数据源,并绑定了新数据及其单元格。但我收到以下错误信息:(如果您有任何报价,请帮助我:

每一段的单元和截面设计是不同的。

错误:线程1:“尝试插入节0,但更新后只有0节”

在这里输入图像描述

在ViewModel文件中:

代码语言:javascript
复制
private func bindSelectedSegmentIndex() {
    /// reset pagination and limit for new request
    selectedSegmentIndex
        .observe(on: MainScheduler.instance)
        .do(onNext: { _ in
            /// remove all old api data in tableviews
            self.transactionsAndDepositsTableViewData.accept([])
            self.contractsTableViewData.accept([])
            self.pagination = Pagination()

            self.updateTableViewDataSource.accept(())
        })
        .subscribe(onNext: { [weak self] _ in
            guard let self = self else {return}
            switch self.selectedSegmentIndex.value {
            case 0,1:
                self.callUserTransactionsAndDeposits()
            case 2:
                self.getContracts()

            default:
                return
            }
            
        })
        .disposed(by: disposeBag)
}

在ViewController中:

代码语言:javascript
复制
@IBAction func segmentControlChanged(_ sender: UISegmentedControl) {
    self.hapticImpactMedium()
    let selectedIndex = sender.selectedSegmentIndex
    self.viewModel.selectedSegmentIndex.accept(selectedIndex)
}


fileprivate func setupTransactionsAndDepositsDataSource() {
    transactionsTableViewDataSource = TableViewSectionedAnimatedDataSourceWithRx(cell: WithdrawAndDepositCell.self,
                                                                                 data: WithdrawAndDepositSection.self)
    
    transactionsTableViewDataSource?.handleCell = { cell ,item in
        cell.item = item
    }
    
    transactionsTableViewDataSource?.dataSource.titleForHeaderInSection = { dataSource, index in
        return dataSource.sectionModels[index].header
    }
}

fileprivate func setupContractsDataSource() {
    contractsTableViewDataSource = TableViewSectionedAnimatedDataSourceWithRx(cell: ContractTableViewCell.self,
                                                                              data: ContractTableSection.self)
    
    contractsTableViewDataSource?.handleCell = { cell ,item in
        cell.item = item
    }
    
    contractsTableViewDataSource?.dataSource.titleForHeaderInSection = { dataSource, index in
        return dataSource.sectionModels[index].header
    }
}

private func setDataSources(with index: Int) {
    /// remove old dataSource and update new one
    tableView.dataSource = nil
    tableView.delegate   = nil
    switch index {
    case 0,1 :
        setupTransactionsAndDepositsDataSource()
        /// Bind tableViewData to the tableView items for transactionsTableViewDataSource
        viewModel.transactionsAndDepositsTableViewData.asDriver()
            .drive(tableView.rx.items(dataSource: transactionsTableViewDataSource.dataSource))
            .disposed(by: disposeBag)
        
    case 2:
        setupContractsDataSource()
        /// Bind tableViewData to the tableView items for clientsTableViewDataSource
        viewModel.contractsTableViewData.asDriver()
            .drive(tableView.rx.items(dataSource: contractsTableViewDataSource.dataSource))
            .disposed(by: disposeBag)
        
    default : break
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-07 11:20:23

我为此添加了段控制,并删除了所有表视图当前数据,并删除了当前数据源,并绑定了新数据及其单元格。

别干那事。您应该只绑定到为数据源提供信息的单个可观察到的对象。编写那个数据源这样它就可以处理任何一种视图模型..。

考虑一个枚举:

代码语言:javascript
复制
enum Section { 
    case withdrawAndDeposit(WidthdrawAndDepositSection)
    case contractTable(ContractTableSection)
}

您发布的代码片段还有许多其他问题,需要更多关于如何使用Rx的教程,而不是这样的答案。我建议您订阅RxSwift板并了解有关如何使用该系统的更多信息。

问题的例子:

  • 在视图模型中过度使用继电器。
  • DisposeBag视图模型。
  • 视图控制器中UIControl (@IBAction)和Rx的混合。
  • 在类中不必要地保留对象。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71772115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档