ios - 反应性在 UITableViewCell
问题描述
我正在尝试从 a 添加删除单元格,UITableView
具体取决于UISegmentedControl
其中一个内的a 中的值变化UITableViewCell
。
问题是存在循环依赖,并且 tableView 不断重新加载。我通过使用避免了这种无限重新加载,distinctUntilChanged
但有时重新加载会被调用两次。
我的目标是根据在段控件中所做的选择来更改行数。
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
推荐阅读
- oracle - 如何使用 runAlways SQL changeSet
- reactjs - .map() 迭代对象数组
- python - 强化学习 - 动作数量
- android - 如何用颤振建立一个wifi信号流?
- swift - 来自@IBInspectable 的 Swift 键盘选择
- cookies - HttpOnly 和 Signed Cookie 的区别
- r - 如何将复杂的文本文档转换为单个字符串
- regex - 正则表达式从路径中选择最后一个单词
- azure - Azure Web App for Containers - Auto Scale 如何处理内部服务器存储?
- ember-data - 如何处理 ember-data 中已弃用的“就绪”模型生命周期挂钩?