【问题标题】:RxDataSources - How to add a custom empty cell when there's no dataRxDataSources - 如何在没有数据时添加自定义空单元格
【发布时间】:2017-09-07 16:44:25
【问题描述】:
struct MyViewModel {
    var items: Observable<String>
    //....
}

// In view controller
viewModel.items.bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: MyCell.self)) { index, model, cell in
  //...
}
.disposed(by: disposeBag)

如果我有另一个名为EmptyCell 的单元格,并且如果项目为空,我想显示此单元格。我怎么能做到这一点。

【问题讨论】:

    标签: rxdatasources


    【解决方案1】:

    RxDataSources 数据源应包含您希望在单元格中显示的任何状态或数据。出于这个原因,您实际上可能希望为您的 SectionItem 提供一个枚举,而不是一个简单的字符串。

    enum CellType {
        case empty
        case regular(String)
    }
    
    typealias Section = SectionModel<String, CellType>
    

    然后,当绑定您的“CellType”Observable 时,您可以相对轻松地使用configureCell Cell Factory 来定义您想要为每种情况出列的单元格。

    例如

    dataSource.configureCell = { _, _, _, cellType in
        switch cellType {
            case .empty: /// Dequeue empty cell
            case .regular(let string): // Dequeue regular cell and set string
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2015-12-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多