Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 159 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RxSwift table view with multiple custom cell types

#1
I'w wonder is there any code example for `RxSwift` when I can use multiple custom cells inside one table view. So for example I have two section and first section has 10 cells with type `CellWithImage` identifier and second section has 10 cells with type `CellWithVideo` identifier.

All tuts and code examples which I've founded are using only one cell type, for instance [RxSwiftTableViewExample][1]

Thanks for any help


[1]:

[To see links please register here]

Reply

#2
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
}
}
Reply

#3
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)
Reply

#4
I've managed it using [RxSwiftDataSources][1],

it allow you to use custom cells with multi sections.
[I've used this code for help][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through