RxSwift、RxCocoa 和 UITableview
RxSwift, RxCocoa and UITableview
我在使用 RxSwift 实现 UITableView 时遇到问题。
我尝试使用以下代码将模型数组的可观察对象绑定到 table 项。
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
上显示的示例并没有什么不同
我创建了一个有错误的小示例项目。
Swift 是非常好的语言,但有时会发生编译器无法识别参数类型的情况。然后你需要显式定义一种参数。在您的情况下,您需要定义块参数的类型,请参阅代码:
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)
}
我在使用 RxSwift 实现 UITableView 时遇到问题。
我尝试使用以下代码将模型数组的可观察对象绑定到 table 项。
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
我创建了一个有错误的小示例项目。
Swift 是非常好的语言,但有时会发生编译器无法识别参数类型的情况。然后你需要显式定义一种参数。在您的情况下,您需要定义块参数的类型,请参阅代码:
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)
}