Jeśli ktoś jest zainteresowany, oto moja implementacja. Mam aplikację z listą gier. W zależności od tego, czy gra jest już skończona, czy nadal używam różnych komórek. Oto mój kod:
W ViewModel mam listę grze, podzielić je na ukończone/trwających te i mapować je do 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)
Następnie w ViewController, wiążę swoje sekcje do mojego tableview i użyj różnych komórek takich jak ta. Zauważ, że mogę użyć IndexPath, aby uzyskać właściwą komórkę zamiast statusu.
vm.gameSections.asObservable().bindTo(tableView.rx.items(dataSource: vm.dataSource)).addDisposableTo(bag)
vm.dataSource.configureCell = {[weak self] (ds, tv, ip, item) -> UITableViewCell in
if item.status == .finished {
let cell = tv.dequeueReusableCell(withIdentifier: "FinishedGameCell", for: ip) as! FinishedGameCell
cell.nameLabel.text = item.opponent.shortName
return cell
} else {
let cell = tv.dequeueReusableCell(withIdentifier: "OnGoingGameCell", for: ip) as! OnGoingGameCell
cell.titleLabel.text = item.opponent.shortName
return cell
}
}
Czy możesz udostępnić kod, jak wdrożyłeś to mimo wszystko? – nburk