Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KananAbilzada opened this issue Apr 6, 2022 · 0 comments

Comments

@KananAbilzada
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant