RxDataSources - How to add a custom empty cell when there's no data

1.8k views Asked by At
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

There are 1 answers

0
Shai Mishali On BEST ANSWER

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
    }
}