Skip to content

Events in Cells

Anton Goncharov edited this page May 12, 2020 · 4 revisions

RxDataSourcesExt lets you process any event in a cell in a convenient way.

In case when you need to know when a specific cell is tapped just use func viewModelSelected from UITableView or UICollectionView. Note that this method expects viewModel class.

tableView.rx.viewModelSelected(StringCellViewModel.self)
            .subscribe(onNext: { value in
                print(text)
            })
            .disposed(by: disposeBag)

If you need some event that happened inside a cell (button tap, text change, etc.) you have 4 methods named cellCreated in director. The first one lets you know when your cell is actually created.

 director.rx.cellCreated(CustomCell.self)
            .subscribe(onNext: { cell, item, indexPath in
            })
            .disposed(by: disposeBag)

The next ones take a closure where you can form observable that you are interested in.

 director.rx.cellCreated(ButtonCell.self) { $0.button.rx.tap }
            .subsribe()
            .disposed(by: disposeBag)

 director.rx.cellCreated(ButtonCell.self) { cell, viewModel in
                cell.button.rx.tap.map { viewModel }
            }
            .subsribe()
            .disposed(by: disposeBag)

 director.rx.cellCreated(ButtonCell.self) { cell, viewModel, index in
                cell.button.rx.tap.map { viewModel }
            }
            .subsribe()
            .disposed(by: disposeBag

Note that every method expects cell class, not viewModel.

Clone this wiki locally