2016-08-22 4 views
9

RxSwiftのコード例は、1つのテーブルビュー内に複数のカスタムセルを使用できるのですか?たとえば、私は2つのセクションを持ち、最初のセクションにはタイプがCellWithImageの10個のセルがあり、2番目のセクションにはタイプがCellWithVideoの10個のセルがあります。私が設立しました複数のカスタムセルタイプを持つRxSwiftテーブルビュー

すべてtutsとコード例は、例えば、唯一の細胞型を使用している、私はそれができるようRxSwiftDataSources

を使用して、それを管理している任意のヘルプ

答えて

3

ためRxSwiftTableViewExample

感謝カスタムセルを複数のセクションで使用することができます。

+4

結局、これをどのように実装したかコードを共有できますか? – nburk

2

誰か興味がある場合は、ここに私の実装です。私はゲームのリストを持つアプリを持っています。ゲームが終了したかどうかによって、私は別のセルを使います。ここに私のコードです:ViewModelにで

、私は終了/継続的なものにそれらを分割し、ゲームのリストを持っていると、私は私のテーブルビューに、私のセクションをバインドのViewControllerに続いて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) 

にマップこのように異なるセルを使用してください。 indexPathを使用して、ステータスの代わりに正しいセルを取得できることに注意してください。

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 
    } 
} 
関連する問題