เผื่อใครสนใจ นี่คือการดำเนินการของฉัน ฉันมีแอพที่มีรายชื่อเกม ขึ้นอยู่กับว่าเกมจบหรือยังดำเนินต่อไป ฉันใช้เซลล์อื่น นี่คือรหัสของฉัน:
ใน ViewModel ฉันมีรายชื่อเกม แบ่งออกเป็นเกมที่เสร็จแล้ว/กำลังดำเนินการ และแมปเกมเหล่านั้นไปที่ 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)
จากนั้นใน ViewController ฉันผูกส่วนต่างๆ ของฉันเข้ากับมุมมองตาราง และใช้เซลล์ต่างๆ เช่นนี้ โปรดทราบว่าฉันสามารถใช้ indexPath เพื่อรับเซลล์ที่ถูกต้องแทนที่จะเป็นสถานะ
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
}
}
person
streem
schedule
09.01.2017