Cannot set bind(to: UITableView) with RxSwift Variable asObservable()

3.5k views Asked by At

I'm trying to bind(to:) a collectionView, but the tableView doesn't work either. I have a viewModel where is my Variable<[]> and I want to subscribe when the value changes, with my tableView.

viewModel.theVariable
        .asObservable()
        .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)){
            (row, item, cell) in
            cell.textLabel?.text = item
        }
        .addDisposableTo(disposeBag)

The XCode tells me Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible' which should be since it's applicable to any UIView.

I've tried Observable with it's just() and that approach seemed to work correctly. The thing is that I need to have a Variable which I set a value in the viewModel and in the View i need to observe this change. Not sure if Observable serves this method.

The point is that this should work even with Variable? Is it a bug? Im using Swift 3.2

2

There are 2 answers

3
CZ54 On BEST ANSWER

Here is a working code example :

    var dataSource : PublishSubject<[String]> = PublishSubject()

    dataSource.asObservable().bind(to: self.mProductsCollectionView.rx.items) { (collectionView, row, element ) in
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseIdentifier", for: IndexPath(row : row, section : 0))
        //customize cell
        return cell

    }.addDisposableTo(bag)

    publish.onNext(["blah", "blah", "blah"])
0
urvashi bhagat On

Here is working code in Swift 3

 var countries = Variable([Country]())
 countries.asObservable()
  .bind(to: tblRx.rx.items(cellIdentifier: "RxCell", cellType: RxCell.self)) { (row, element, cell) in
            //customise cell here
        }
     .disposed(by: disposeBag)