RxDataSources - 如何在没有数据时添加自定义空单元格

时间:2017-09-07 01:29:03

标签: rxdatasources

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

1 个答案:

答案 0 :(得分:7)

RxDataSources数据源应包含要在单元格中显示的任何状态或数据。因此,您可能希望对SectionItem进行枚举,而不是简单的字符串。

enum CellType {
    case empty
    case regular(String)
}

typealias Section = SectionModel<String, CellType>

然后,绑定你的&#34; CellType&#34;可观察,您可以相对轻松地使用configureCell Cell Factory来定义您希望为每种情况出列的单元格。

e.g。

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