Keith Randell Gapusan
Keith Randell Gapusan

Reputation: 83

How to implement in RXswift and RXCocoa of UITableView Inside of tableview cell?

I'm new to RXswift and currently working in a project that requires rxswift with UITableView inside of UITableViewCell

Below are some of the codes I've done so far, but every time scroll up/down, it doesn't show the right value

    var disposeBag = DisposeBag()

    viewModel.listExperience.asObservable().bind(to: tableView.rx.items(cellIdentifier: cellExperienceTableViewCell.getCellId(), cellType: ExperienceTableViewCell.self)){
            row , data, cell in

             cell.lblTitle.text = data.title ?? ""
             cell.lblDate.text = data.startDate ?? ""
             cell.lblDetails.text = data.body ?? ""

            let filtered = self.viewModel.getDataCommentInRow(row) /* returns Variable<[Comments]> */
             cell.setCommentTableView(row: row, filtered: filtered)
             cell.tableViewOffset = self.storedOffsets[row] ?? 0
             cell.tableViewComments.reloadData()

           }.disposed(by: disposeBag)

here's the code of the tableView inside of my tableView cell

  func setCommentTableView(row : Int, filtered  : Variable<[ExperienceComment]>){

    tableViewComments.tag = row
    filtered.asObservable().bind(to: tableViewComments.rx.items(cellIdentifier: commentCell.getCellId(), cellType: CommentCell.self)){  indexPath , data, cell in
        cell.lblComment.text = data.body
      }.disposed(by: disposeBag)
}

Anyone knows how to achieve this? thanks

Upvotes: 1

Views: 1218

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

I think that the only thing you have to do is add a prepareForReuse method in your table view cell. In it add disposeBag = DisposeBag(). Make sure your DisposeBag is a var inside the cell in order to do this.

If I'm right, the problem is that when the cell is getting reused, it starts reading from multiple filtered observables because they all get bound to the same tableViewComments.

Upvotes: 1

Related Questions