【问题标题】:RxSwift, MVVM - Cannot implement UITableViewDataSource methods with RxSwift bindingRxSwift,MVVM - 无法使用 RxSwift 绑定实现 UITableViewDataSource 方法
【发布时间】:2017-05-28 18:08:16
【问题描述】:

我正在使用 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.

我用谷歌搜索,发现我必须将 tableView.delegate 和 tableView.dataSource 设置为 nil。

但是,我仍然需要一些 UITableViewDataSource 方法,例如

canEditRowAtIndexPath
commitEditingStyle

用于删除地址。

我应该如何实现这个?提前致谢。

【问题讨论】:

    标签: ios swift mvvm reactive-programming rx-swift


    【解决方案1】:

    在上面写下这一行:

    tableView.dataSource = nil
    

    【讨论】:

    • 啊,不,那行不通。它会填充你的单元格,但不能帮助你实现我上面提到的删除动画功能。
    • 那么你会想要使用RxDataSources 或查看rx-example
    • 没错,我自己找不到替代解决方案。谢谢大佬!
    【解决方案2】:

    好的,我找到了解决方案。

    使用 RxDataSource。

    https://github.com/RxSwiftCommunity/RxDataSources

    我真的不想为此使用新的 pod,但我找不到替代解决方案。

    好的,我现在确实找到了替代解决方案!

    使用

    tableView.rx_itemDeleted
    

    这个函数是 ReactSwift 对 commitEditingStyle 的封装(误导是的!),无论你在 commitEditingStyle 中写什么,你现在都可以在这个函数中写。对于canEditRowAtIndexPath,我一直想返回true,这是默认的返回值,即使我不为此编写代码,所以我根本不需要实现canEditRowAtIndexPath!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多