RxDataSources - How to add a custom empty cell whe

2020-07-24 04:27发布

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)

If I have a another cell called EmptyCell, and I want to display this cell if the items is empty. How could I achieve this.

1条回答
劫难
2楼-- · 2020-07-24 04:55

The RxDataSources data source should consist of any piece of state or data you want to display in your cells. For this reason you might actually want to have an enumeration for your SectionItem and not a simple string.

enum CellType {
    case empty
    case regular(String)
}

typealias Section = SectionModel<String, CellType>

Then, when binding your "CellType" Observable, you can relatively easily use the configureCell Cell Factory to define which cell you would want to dequeue for each case.

e.g.

dataSource.configureCell = { _, _, _, cellType in
    switch cellType {
        case .empty: /// Dequeue empty cell
        case .regular(let string): // Dequeue regular cell and set string
    }
}
查看更多
登录 后发表回答