简体   繁体   English

RxSwift、MVVM - 无法使用 RxSwift 绑定实现 UITableViewDataSource 方法

[英]RxSwift, MVVM - Cannot implement UITableViewDataSource methods with RxSwift binding

I am implementing a simple UITableView of addresses using RxSwift and MVVM.我正在使用 RxSwift 和 MVVM 实现一个简单的 UITableView 地址。 I have created a binding in my view controller like this.我在我的视图控制器中创建了一个这样的绑定。

viewModel.addressList.asDriver()
   .drive(tableView.rx_itemsWithCellIdentifier(reusableIdentifier, cellType: SavedAddressTableViewCell.self)) { [weak self] (row, viewModel, cell) in
        self?.setUpAddressCell(cell, row: (row + 1))
    }
    .addDisposableTo(disposeBag)

However, I keep getting this error when I try to open the page in my app.但是,当我尝试在我的应用程序中打开页面时,我不断收到此错误。

Maybe delegate was already set in `xib` or `storyboard` and now it's being overwritten in code.

I Googled and found that I have to set tableView.delegate and tableView.dataSource to nil.我用谷歌搜索,发现我必须将 tableView.delegate 和 tableView.dataSource 设置为 nil。

However, there are some UITableViewDataSource methods that I still need, like但是,我仍然需要一些 UITableViewDataSource 方法,例如

canEditRowAtIndexPath
commitEditingStyle

for deleting addresses.用于删除地址。

How should I implement this?我应该如何实施? Thanks in advance.提前致谢。

在上面写这一行:

tableView.dataSource = nil

Okay I found the solution.好的,我找到了解决方案。

Use RxDataSources.使用 RxDataSource。

https://github.com/RxSwiftCommunity/RxDataSources https://github.com/RxSwiftCommunity/RxDataSources

I really didn't want to use a new pod for this, but I couldn't find an alternative solution.我真的不想为此使用新的吊舱,但我找不到替代解决方案。

Okay I did find an alternative solution now!好的,我现在确实找到了替代解决方案!

Use

tableView.rx_itemDeleted

This function is ReactSwift's wrapper for commitEditingStyle (misleading yes!), whatever you would have written in commitEditingStyle you can now write in this function.这个函数是 ReactSwift 的 commitEditingStyle 包装器(是误导!),无论你在 commitEditingStyle 中写什么,你现在都可以在这个函数中写。 For canEditRowAtIndexPath, I always wanted to return true, which is the default return value even if I don't write code for that, so I didn't have to implement canEditRowAtIndexPath at all!对于canEditRowAtIndexPath,我一直想返回true,这是默认返回值,即使我不为此编写代码,所以我根本不必实现canEditRowAtIndexPath!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM