2
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,并且如果项目为空,我想显示此单元格。我怎么能做到这一点。

4

1 回答 1

7

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

enum CellType {
    case empty
    case regular(String)
}

typealias Section = SectionModel<String, CellType>

然后,当绑定你的“CellType”Observable 时,你可以相对容易地使用configureCellCell Factory 来定义你想要为每个案例出列的单元格。

例如

dataSource.configureCell = { _, _, _, cellType in
    switch cellType {
        case .empty: /// Dequeue empty cell
        case .regular(let string): // Dequeue regular cell and set string
    }
}
于 2017-09-07T08:44:25.117 回答