2

I am learning rxswift and I have a ViewController with a tableView.
I am using the MVVM architecture, so a tableView is bound to a ViewModel and it displays data well.

But I want put a TableView label empty state when I don't have data and I don't know how to do it, if it's possible.

This is my code so far:

func bindViewModel() {    
    let inputs = viewModel.inputs
    let outputs = viewModel.outputs
    btnSomeAction.rx.action = inputs.someAction
    outputs.usersList
        .bind(to: tableView.rx.items(cellIdentifier: "UserTableViewCell", cellType: UserTableViewCell.self)) { (_,element,cell) in
            cell.setData(name: element.name, avatar: element.avatar)
    }
    .disposed(by: disposeBag)
}

Thanks to all for any help.
P.S.:
I know how to do it in a general table without rx with amount of element zero. The question is about rxtable.

zx485
  • 28,498
  • 28
  • 50
  • 59

1 Answers1

2

The easiest solution would be converting the observable to a Driver, and returning the empty-state model like this:

   outputs.usersList
    .asDriver(onErrorJustReturn: [])
    .drive(to: tableView.rx.items(cellIdentifier: "UserTableViewCell", cellType: UserTableViewCell.self)) { (_,element,cell) in
        cell.setData(name: element.name, avatar: element.avatar)
}
.disposed(by: disposeBag)

As far as I know, a more complex solution would be using RxDataSources RxDataSources - How to add a custom empty cell when there's no data

Adrian
  • 415
  • 4
  • 18