2017-09-13 66 views
1

我有一个使用RxSwift实现UITableView的问题。RxSwift,RxCocoa和UITableview

我试图使用下面的代码将模型数组的observable绑定到表项。 models.bind(to: self.tableView.rx.items(cellIdentifier: "Cell", cellType: ModelTableViewCell.self

但是,当我这样做给我以下错误:Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible',我知道错误不能正确,因为NSObject扩展ReactiveCompatible,所以UITableView也。另外,我的项目代码与RxSwiftCommunity

上显示的示例没有太大区别。我创建了一个有错误的小示例项目。

[Example code showing the error (picture)]

回答

2

斯威夫特是相当不错的语言,但有时会发生的时刻,当编译器无法识别的参数类型。那么你需要明确定义一个参数类型。你的情况,你需要定义的块参数的类型,看到代码:

func bindRx(viewModel: ViewModel) { 
    viewModel.models.bind(to: tableView.rx.items(cellIdentifier: ModelTableViewCell.ReuseIdentifier, 
               cellType: ModelTableViewCell.self)) { (_, model: Model, cell: ModelTableViewCell) in 
     cell.textLabel?.text = model.name 
    } 
    .addDisposableTo(disposeBag) 
} 

enter image description here