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

故事扮演 提交于 2019-12-25 00:34:26

问题


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


回答1:


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



回答2:


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)


来源:https://stackoverflow.com/questions/46130494/cannot-set-bindto-uitableview-with-rxswift-variable-asobservable

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