RxSwift、RxCocoa 和 UITableview [英] RxSwift, RxCocoa and UITableview

查看:34
本文介绍了RxSwift、RxCocoa 和 UITableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 RxSwift 实现 UITableView 时遇到问题.

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

但是,当我这样做时,会出现以下错误:Type 'inout UITableView' 不符合协议 'ReactiveCompatible' 我知道错误不可能是正确的,因为 NSObject 扩展了 ReactiveCompatible 所以 UITableView也可以.此外,我的项目代码与

解决方案

Swift 是一种非常好的语言,但有时会发生编译器无法识别参数类型的情况.然后你需要显式定义一个参数类型.在您的情况下,您需要定义块参数的类型,请参阅代码:

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

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.

解决方案

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

这篇关于RxSwift、RxCocoa 和 UITableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆