ios - UITableViewCell 中的 Reactive<UISegmentedControl>

标签 ios swift uitableview rx-swift

我正在尝试根据 UITableViewCell 中的 UISegmentedControl 中的值更改从 UITableView 添加删除单元格。

问题是存在循环依赖,tableView 一直在重新加载。我通过使用 distinctUntilChanged 避免了这种无限重新加载,但有时仍然会调用两次重新加载。

我的目标是根据段控件中的选择更改行数。

Code in UITableViewCell


extension CardTableViewCell {

    /// This function is called every time in `cellForRowAtIndexPath` for the tableView.

    func bind(viewModel: TableViewViewModel?) {
        self.viewModel = viewModel
        if let viewModel = viewModel {
            segment.rx.selectedSegmentIndex
                .asDriver()
                .flatMap { Driver.just($0 == 0)}
                .distinctUntilChanged()
                .drive(onNext: { value in

                    print(">>>> VALUE CHANGED \(value)")
                    viewModel.reloadTableView()}

                // Here when I reload the table view this is called again and a cyclic dependency is created

                ) >>> disposeBag
        }
    }
}

The console returns this

⚠️ Reentrancy anomaly was detected.
  > Debugging: To debug this issue you can set a breakpoint in /Users/harshvishwakarma/Documents/GitHub/banking-app-ios/Pods/RxSwift/RxSwift/Rx.swift:97 and observe the call stack.
  > Problem: This behavior is breaking the observable sequence grammar. `next (error | completed)?`
    This behavior breaks the grammar because there is overlapping between sequence events.
    Observable sequence is trying to send an event before sending of previous event has finished.
  > Interpretation: This could mean that there is some kind of unexpected cyclic dependency in your code,
    or that the system is not behaving in the expected way.
  > Remedy: If this is the expected behavior this message can be suppressed by adding `.observeOn(MainScheduler.asyncInstance)`
    or by enqueing sequence events in some other way.

最佳答案

您将需要使用类似[RxDataSources][1] 的库或以其他方式编写您自己的数据源类。默认数据源的行为是在 TableView 上调用 reloadData(),但您需要一个能够按需追加/删除单元格而不是始终加载整个 tableView 的行为。

如果你觉得RxDataSources重量太重,那你可以自己写一个简单的数据源。这是一个例子:https://github.com/danielt1263/RxMultiCounter/blob/master/RxMultiCounter/RxExtensions/RxSimpleAnimatableDataSource.swift

关于ios - UITableViewCell 中的 Reactive<UISegmentedControl>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369077/

相关文章:

iOS 8 : how to set height programmatically for a UITableview in a freeform ViewController

c++ - 在 cocos2d-x 上使用参数从 Object-C 到 C++ 的函数回调

objective-c - 为什么 iOS main.m 模板包含返回语句和自动释放池?

ios - 如何使用 Swift 在屏幕中央水平或垂直设置 UIImageView,但不再使用 StoryBoard

ios - 如何使用 swift 在 ios 中获取方形裁剪照片缩略图

iphone - UITableView - 为多行部分创建单个删除按钮

ios - waitUntilAllTask​​sAreFinished 错误 Swift

swift - 字符串转 UTF-32 字符串

ios - 在 iOS 中从 NSArray 的 UITableView 删除一行并更新 UI

jquery - 具有 4 列(所有屏幕宽度)和 2 行应用翻转效果 jquery 的表格