2016-08-22 10 views
9

Zastanawiam się, czy istnieje przykład kodu dla RxSwift, gdy mogę używać wielu niestandardowych komórek w jednym widoku tabeli. Na przykład mam dwie sekcje i pierwsza sekcja ma 10 komórek z identyfikatorem typu CellWithImage, a druga sekcja ma 10 komórek z identyfikatorem typu CellWithVideo.Widok tabeli RxSwift z wieloma niestandardowymi typami komórek

Wszystkie tuts i przykłady kodu, które mam założone są przy użyciu tylko jednego typu komórek, na przykład RxSwiftTableViewExample

Dzięki za wszelką pomoc

Odpowiedz

3

udało mi go za pomocą RxSwiftDataSources,

to pozwolić możesz używać niestandardowych komórek z wieloma sekcjami.

+4

Czy możesz udostępnić kod, jak wdrożyłeś to mimo wszystko? – nburk

2

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 
    } 
} 
Powiązane problemy