RxSwift、MVVM - 无法使用 RxSwift 绑定实现 UITableViewDataSource 方法
RxSwift, MVVM - Cannot implement UITableViewDataSource methods with RxSwift binding
我正在使用 RxSwift 和 MVVM 实现一个简单的地址 UITableView。我像这样在我的视图控制器中创建了一个绑定。
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)
但是,当我尝试在我的应用程序中打开该页面时,我总是收到此错误。
Maybe delegate was already set in `xib` or `storyboard` and now it's being overwritten in code.
我用 Google 搜索发现我必须将 tableView.delegate 和 tableView.dataSource 设置为 nil。
但是,我仍然需要一些 UITableViewDataSource 方法,例如
canEditRowAtIndexPath
commitEditingStyle
用于删除地址。
我该如何实施?提前致谢。
在上面写下这一行:
tableView.dataSource = nil
好的,我找到了解决方案。
使用 RxDataSources。
https://github.com/RxSwiftCommunity/RxDataSources
我真的不想为此使用新的 pod,但我找不到替代解决方案。
好的,我现在确实找到了替代解决方案!
使用
tableView.rx_itemDeleted
这个函数是 ReactSwift 对 commitEditingStyle 的包装(误导是!),无论你在 commitEditingStyle 中写什么,你现在都可以在这个函数中写。对于 canEditRowAtIndexPath,我一直希望 return true,这是默认的 return 值,即使我不为此编写代码,所以我根本不必实现 canEditRowAtIndexPath!
我正在使用 RxSwift 和 MVVM 实现一个简单的地址 UITableView。我像这样在我的视图控制器中创建了一个绑定。
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)
但是,当我尝试在我的应用程序中打开该页面时,我总是收到此错误。
Maybe delegate was already set in `xib` or `storyboard` and now it's being overwritten in code.
我用 Google 搜索发现我必须将 tableView.delegate 和 tableView.dataSource 设置为 nil。
但是,我仍然需要一些 UITableViewDataSource 方法,例如
canEditRowAtIndexPath
commitEditingStyle
用于删除地址。
我该如何实施?提前致谢。
在上面写下这一行:
tableView.dataSource = nil
好的,我找到了解决方案。
使用 RxDataSources。
https://github.com/RxSwiftCommunity/RxDataSources
我真的不想为此使用新的 pod,但我找不到替代解决方案。
好的,我现在确实找到了替代解决方案!
使用
tableView.rx_itemDeleted
这个函数是 ReactSwift 对 commitEditingStyle 的包装(误导是!),无论你在 commitEditingStyle 中写什么,你现在都可以在这个函数中写。对于 canEditRowAtIndexPath,我一直希望 return true,这是默认的 return 值,即使我不为此编写代码,所以我根本不必实现 canEditRowAtIndexPath!