Bartando
Bartando

Reputation: 748

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

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

Upvotes: 1

Views: 3625

Answers (2)

urvashi bhagat
urvashi bhagat

Reputation: 1163

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)

Upvotes: 1

CZ54
CZ54

Reputation: 5588

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"])

Upvotes: 7

Related Questions