1

我正在尝试构建一个 tableView 女巫有许多带有按钮的单元格,我想要做的是当我单击单元格中的按钮时,单元格应该转到表格的底部,这是我的代码:

let datasource = RxTableViewSectionedAnimatedDataSource<ToDoListSection>(
            configureCell: { [weak self] _, tableView, indexPath, item in
                guard let self = self else { return UITableViewCell() }
                let cell = tableView.dequeueReusableCell(withIdentifier: ToDoTableViewCell.reuseID, for: indexPath) as? ToDoTableViewCell
                cell?.todoTextView.text = item.text
                cell?.checkBox.setSelect(item.isSelected)
                cell?.checkBox.checkBoxSelectCallBack = { selected in
                    if selected {
                        var removed = self.datasList[indexPath.section].items.remove(at: indexPath.row)
                        removed.isSelected = selected
                        self.datasList[indexPath.section].items.append(removed)
                        self.datasList[indexPath.section] = ToDoListSection(
                            original: self.datasList[indexPath.section],
                            items: self.datasList[indexPath.section].items
                        )
                        self.sections.onNext(datasList)
                    } else {
                        // Todo
                    }
                }
                return cell ?? UITableViewCell()
            }, titleForHeaderInSection: { dataSource, section in
                return dataSource[section].header
            })
        
        sections.bind(to: table.rx.items(dataSource: datasource))
            .disposed(by: disposeBag)

但是,因为我在闭包中发送了 onNext 事件configureCell,所以收到了警告:

⚠️ 检测到重入异常。

调试:要调试此问题,您可以在 /Users/me/Desktop/MyProject/Pods/RxSwift/RxSwift/Rx.swift:96 中设置断点并观察调用堆栈。问题:这种行为破坏了可观察序列语法。next (error | completed)? 这种行为破坏了语法,因为序列事件之间存在重叠。可观察序列试图在前一个事件的发送完成之前发送一个事件。解释:这可能意味着您的代码中存在某种意外的循环依赖,或者系统未按预期方式运行。补救措施:如果这是预期的行为,则可以通过添加.observe(on:MainScheduler.asyncInstance) 或以其他方式将序列事件排入队列来抑制此消息。

屏幕上的动作不是我想要的。我应该怎么办?如何正确重新加载 TableView?

4

1 回答 1

0

这里的基本问题是你onNext在观察发射的观察者内部调用。另一种说法是,您在系统完成处理当前值之前发出一个新值。

正如警告所说,处理这个问题的最简单方法(在这种情况下可能是最好的方法)是在and.observe(on:MainScheduler.asyncInstance)之间插入。它的作用是将发射停止一个周期,以便您的函数有机会返回。sections.bind(to:)configureCell

于 2022-03-04T16:20:37.683 回答