我正试图根据UITableView中aUISegmentedControl的值变化(位于其中一个UITableViewCell中)从adistinctUntilChanged中添加remove cells。
问题是存在循环依赖关系,而tableView一直在重新加载。我通过使用避免了这种无限的重新加载,但有时仍然会调用两次重新加载。
我的目标是根据在段控件中所做的选择更改行数。
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
        }
    }
}

控制台返回这个
⚠️ 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]的库,或者编写自己的数据源类。默认数据源的行为是在表视图上调用reloadData(),但您需要一个按需追加/删除单元格的数据源,而不是总是加载整个表视图。
如果您觉得RxDataSources太重,那么您可以自己编写一个简单的数据源。下面是一个例子:https://github.com/danielt1263/RxMultiCounter/blob/master/RxMultiCounter/RxExtensions/RxSimpleAnimatableDataSource.swift

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

10-10 21:20