RxSwift table view with multiple custom cell types
You can set multiple custom cell without RxDatasource.
//Register Cells as you want
tableView.register(CustomRxTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "BasicCell")
ViewModel.data.bind(to: tableView.rx.items){(tv, row, item) -> UITableViewCell in
if row == 0 {
let cell = tv.dequeueReusableCell(withIdentifier: "BasicCell", for: IndexPath.init(row: row, section: 0))
cell.textLabel?.text = item.birthday
return cell
}else{
let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: IndexPath.init(row: row, section: 0)) as! CustomRxTableViewCell
cell.titleLb.text = item.name
return cell
}
}.disposed(by: disposeBag)
In case anybody's interested, here's my implementation. I have an app with a list of games. Depending if the game is finished or still on going i use different cells. Here's my code:
In the ViewModel, I have a list of game, split them into finished/ongoing ones and map them to SectionModel
let gameSections = PublishSubject<[SectionModel<String, Game>]>()
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, Game>>()
...
self.games.asObservable().map {[weak self] (games: [Game]) -> [SectionModel<String, Game>] in
guard let safeSelf = self else {return []}
safeSelf.ongoingGames = games.filter({$0.status != .finished})
safeSelf.finishedGames = games.filter({$0.status == .finished})
return [SectionModel(model: "Ongoing", items: safeSelf.ongoingGames), SectionModel(model: "Finished", items: safeSelf.finishedGames)]
}.bindTo(gameSections).addDisposableTo(bag)
Then in the ViewController, I bind my sections to my tableview, and use different cells like this. Note that I could use the indexPath to get the right cell instead of the status.
vm.gameSections.asObservable().bindTo(tableView.rx.items(dataSource: vm.dataSource)).addDisposableTo(bag)
vm.dataSource.configureCell = {[weak self] (datasource, tableview, indexpath, item) -> UITableViewCell in
if item.status == .finished {
let cell = tableview.dequeueReusableCell(withIdentifier: "FinishedGameCell", for: indexpath) as! FinishedGameCell
cell.nameLabel.text = item.opponent.shortName
return cell
} else {
let cell = tableview.dequeueReusableCell(withIdentifier: "OnGoingGameCell", for: indexpath) as! OnGoingGameCell
cell.titleLabel.text = item.opponent.shortName
return cell
}
}
I've managed it using RxSwiftDataSources,
it allow you to use custom cells with multi sections. I've used this code for help