2017-09-13 41 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