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

限于喜欢 提交于 2019-12-06 11:33:23

问题


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:


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


来源:https://stackoverflow.com/questions/46086529/rxdatasources-how-to-add-a-custom-empty-cell-when-theres-no-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!