【问题标题】:RxSwift table view with multiple custom cell types具有多种自定义单元格类型的 RxSwift 表格视图
【发布时间】:2016-12-29 00:03:32
【问题描述】:

当我可以在一个表格视图中使用多个自定义单元格时,我想知道RxSwift 是否有任何代码示例。例如,我有两个部分,第一部分有 10 个单元格类型为 CellWithImage 标识符,第二部分有 10 个单元格类型为 CellWithVideo 标识符。

我创建的所有 tuts 和代码示例都只使用一种单元格类型,例如 RxSwiftTableViewExample

感谢您的帮助

【问题讨论】:

    标签: ios swift uitableview swift2 rx-swift


    【解决方案1】:

    如果有人感兴趣,这是我的实现。我有一个包含游戏列表的应用程序。根据游戏是否完成或仍在进行中,我使用不同的单元格。这是我的代码:

    在 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 中,我将我的部分绑定到我的 tableview,并像这样使用不同的单元格。请注意,我可以使用 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
        }
    }
    

    【讨论】:

    • 如何更新数据源中的项目? dataSource.sectionModels 仅限获取。我的模型是一个数组,我试图在一些用户输入后更新其中一个项目。谢谢
    【解决方案2】:

    您可以在没有 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】:

      我已经使用RxSwiftDataSources 管理它,

      它允许您使用具有多个部分的自定义单元格。 I've used this code for help

      【讨论】:

      • 你能分享一下你是如何实现这个的代码吗?
      • 是吗?我们不能直接用 RxSwift 添加其他表函数吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多