1

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

Bartando
  • 719
  • 8
  • 26

2 Answers2

7

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"])
CZ54
  • 5,488
  • 1
  • 24
  • 39
  • I will try this later today and if this is correct the bounty is yours :) – Bartando Sep 12 '17 at 15:13
  • Any chance to try ? – CZ54 Sep 13 '17 at 09:51
  • 1
    Ok, so the problem seems to be that I'm using my own model which has an array in it. Seems like I need to use the array right away. So I've mapped the array as the input of the bind(to:) function and it seems to work. Maybe it will even work with the Variable. Thank you, the bounty is yours... – Bartando Sep 13 '17 at 18:19
1

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)
urvashi bhagat
  • 1,123
  • 12
  • 15