[英]RxSwift, RxCocoa and UITableview
I have a problem with implementing a UITableView using RxSwift. 我在使用RxSwift实现UITableView时遇到问题。
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
. 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. 但是,当我这样做时,会出现以下错误:输入
Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible'
并且我知道该错误可能不正确,因为NSObject扩展了ReactiveCompatible,所以UITableView也可以。 Also, my project code isn't really different than the examples shown on RxSwiftCommunity 另外,我的项目代码与RxSwiftCommunity上显示的示例并没有什么不同
I created a small example project that has the error. 我创建了一个有错误的小示例项目。
Swift is quite good language but sometimes happens moments when compiler couldn't recognize the type of parameters. Swift是一种非常不错的语言,但是有时会在编译器无法识别参数类型时发生。 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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.