具有多个自定义单元格类型的 RxSwift table 视图
RxSwift table view with multiple custom cell types
我想知道当我可以在一个 table 视图中使用多个自定义单元格时,是否有 RxSwift
的任何代码示例。因此,例如,我有两个部分,第一部分有 10 个单元格,类型为 CellWithImage
标识符,第二部分有 10 个单元格,类型为 CellWithVideo
标识符。
我创建的所有 tuts 和代码示例都只使用一种单元格类型,例如 RxSwiftTableViewExample
感谢您的帮助
我已经使用 RxSwiftDataSources、
管理了它
它允许您使用包含多个部分的自定义单元格。
I've used this code for help
如果有人感兴趣,这是我的实现。我有一个包含游戏列表的应用程序。根据游戏是否完成或仍在进行中,我使用不同的单元格。这是我的代码:
在 ViewModel 中,我有一个游戏列表,将它们分成 finished/ongoing 个并将它们映射到 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({[=10=].status != .finished})
safeSelf.finishedGames = games.filter({[=10=].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
}
}
您可以在没有 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)
我想知道当我可以在一个 table 视图中使用多个自定义单元格时,是否有 RxSwift
的任何代码示例。因此,例如,我有两个部分,第一部分有 10 个单元格,类型为 CellWithImage
标识符,第二部分有 10 个单元格,类型为 CellWithVideo
标识符。
我创建的所有 tuts 和代码示例都只使用一种单元格类型,例如 RxSwiftTableViewExample
感谢您的帮助
我已经使用 RxSwiftDataSources、
管理了它它允许您使用包含多个部分的自定义单元格。 I've used this code for help
如果有人感兴趣,这是我的实现。我有一个包含游戏列表的应用程序。根据游戏是否完成或仍在进行中,我使用不同的单元格。这是我的代码:
在 ViewModel 中,我有一个游戏列表,将它们分成 finished/ongoing 个并将它们映射到 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({[=10=].status != .finished})
safeSelf.finishedGames = games.filter({[=10=].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
}
}
您可以在没有 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)