RxSwiftCommunity / RxDataSources

UITableView and UICollectionView Data Sources for RxSwift (sections, animated updates, editing ...)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Binding multiple data and cells in RxSwiftDataSource (attempt to insert section 0 but there are only 0 sections after the update)

KananAbilzada opened this issue · comments

I'm trying to show multiple cells and their own data models in the same tableView. I added segment control for this and I delete all tableview current data and removed the current data sources and binding new data and its cell. But I am getting the below error message :( If you have any offers please help me :(

P.s: Each segment's cells and sections design is different than each other. Error: Thread 1: "attempt to insert section 0 but there are only 0 sections after the update"

In View Model

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)
}

In ViewController:

@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
    }
}