RxSwift table view with multiple custom cell types

*爱你&永不变心* 提交于 2020-06-10 07:28:26

问题


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

Thanks for any help


回答1:


I've managed it using RxSwiftDataSources,

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




回答2:


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)



回答3:


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] (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
    }
}


来源:https://stackoverflow.com/questions/39079491/rxswift-table-view-with-multiple-custom-cell-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!