首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reactive<UISegmentedControl> in UITableViewCell

Reactive<UISegmentedControl> in UITableViewCell
EN

Stack Overflow用户
提问于 2019-08-06 04:43:58
回答 1查看 648关注 0票数 1

我试图从UITableView中添加删除单元格,具体取决于UISegmentedControl中的值变化,该值位于UITableViewCell的一个内部。

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

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

UITableViewCell中的代码

代码语言:javascript
复制
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
        }
    }
}

控制台返回以下内容

代码语言:javascript
复制
⚠️ 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.
EN

回答 1

Stack Overflow用户

发布于 2019-08-07 01:19:20

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57369077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档