无法使用 RxSwift 变量 asObservable() 设置 bind(to: UITableView) [英] Cannot set bind(to: UITableView) with RxSwift Variable asObservable()

查看:41
本文介绍了无法使用 RxSwift 变量 asObservable() 设置 bind(to: UITableView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定(到:) collectionView,但 tableView 也不起作用.我有一个 viewModel,我的 Variable<[]> 在哪里,我想在值更改时使用我的 tableView 进行订阅.

I'm trying to bind(to:) a collectionView, but the tableView doesn't work either. I have a viewModel where is my Variable<[]> and I want to subscribe when the value changes, with my tableView.

viewModel.theVariable
        .asObservable()
        .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)){
            (row, item, cell) in
            cell.textLabel?.text = item
        }
        .addDisposableTo(disposeBag)

XCode 告诉我 Type 'inout UITableView' 不符合协议 'ReactiveCompatible' 这应该是因为它适用于任何 UIView.

The XCode tells me Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible' which should be since it's applicable to any UIView.

我已经尝试过使用它的 just() Observable 并且这种方法似乎可以正常工作.问题是我需要有一个变量,我在 viewModel 中设置了一个值,在 View 中我需要观察这个变化.不确定 Observable 是否提供此方法.

I've tried Observable with it's just() and that approach seemed to work correctly. The thing is that I need to have a Variable which I set a value in the viewModel and in the View i need to observe this change. Not sure if Observable serves this method.

关键是即使使用变量也应该可以工作?这是一个错误吗?我使用的是 Swift 3.2

The point is that this should work even with Variable? Is it a bug? Im using Swift 3.2

推荐答案

这是一个工作代码示例:

Here is a working code example :

    var dataSource : PublishSubject<[String]> = PublishSubject()

    dataSource.asObservable().bind(to: self.mProductsCollectionView.rx.items) { (collectionView, row, element ) in
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseIdentifier", for: IndexPath(row : row, section : 0))
        //customize cell
        return cell

    }.addDisposableTo(bag)

    publish.onNext(["blah", "blah", "blah"])

这篇关于无法使用 RxSwift 变量 asObservable() 设置 bind(to: UITableView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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