无法使用RxSwift变量asObservable()设置bind(to:UITableView)

时间:2017-09-09 12:05:44

标签: ios swift uitableview rxdatasources

我正在尝试绑定(:) :) collectionView,但tableView也不起作用。我有一个viewModel,其中是我的变量< []>我想用我的tableView在值改变时订阅。

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

XCode告诉我Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible'应该是什么,因为它适用于任何UIView。

我已经尝试了Observable,它只是()并且这种方法似乎正常工作。问题是我需要一个变量,我在viewModel中设置一个值,在View中我需要观察这个变化。不确定Observable是否提供此方法。

重点是,即使使用Variable也可以使用它?这是一个错误吗?我正在使用Swift 3.2

2 个答案:

答案 0 :(得分:4)

这是一个有效的代码示例:

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

答案 1 :(得分:1)

以下是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)