RxSwift, RxCocoa and UITableview

半世苍凉 提交于 2019-12-24 07:50:41

问题


I have a problem with implementing a UITableView using RxSwift.

I tried to bind an observable of an array of models to the table items with the following code. models.bind(to: self.tableView.rx.items(cellIdentifier: "Cell", cellType: ModelTableViewCell.self.

But when I do it gives me the following error: Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible' and I know the error can't be right because NSObject extends ReactiveCompatible so UITableView also does. Also, my project code isn't really different than the examples shown on RxSwiftCommunity

I created a small example project that has the error.


回答1:


Swift is quite good language but sometimes happens moments when compiler couldn't recognize the type of parameters. Then you need to explicit define a type of arguments. In your case you need to define the type of block arguments, see the code:

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)
}



来源:https://stackoverflow.com/questions/46201795/rxswift-rxcocoa-and-uitableview

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