UITableViewDiffableDataSource可以检测到更改的项目吗? [英] Can UITableViewDiffableDataSource detect an item changed?

查看:110
本文介绍了UITableViewDiffableDataSource可以检测到更改的项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在与下面的@AndreasOetjen讨论后,问题被重写了.感谢他的评论.)

(The question was rewritten after discussing with @AndreasOetjen below. Thanks for his comments.)

在将UITableView与可扩散数据源一起使用时遇到问题.在我的应用中,当用户修改某个项目时,它可能会更改另一个在同一表格视图中显示的项目.问题是,在我创建并应用了包含两个项目的新值的新快照之后,间接更新的项目的UI并未更新.

I ran into an issue with using UITableView with diffable data source. In my app when user modifies an item, it may change another item which is shown in the same table view. The issue is that, after I created and applied a new snapshot containing both items' new values, the indirectly changed item's UI wasn't updated.

起初,我认为差异数据源能够在不同的快照中检测到项目的值变化.例如,它可能是这样工作的:如果发现两个快照都包含相同的项目(即两个快照中的项目具有相同的哈希值),则将其值进行比较,并在值更改时更新表视图中该项目的行.但是,我后来意识到它可能无法正常工作,因为差异数据源未定义任何API来获取和比较项目值(我最初的想法是它使用了description计算属性和==操作,但是现在我相信这是不正确的.

At first I thought diffable data source was able to detect an item's value change in different snapshot. For example, it might work this way: if it found both snapshots contains the same item (that is, items in both snapshots have same hash value), it compared their values and updated that item's row in table view if value changed. However, I later realized it perhaps didn't work that way because diffable data source doesn't define any API to get and compare item value (my original thought was it used description computed property and == operation, but now I believe it's not true).

所以我目前的理解是可变数据源使用项目的哈希值来检测项目订单更改(即,插入了新项目,旧项目仍然存在等),而不是项目值更改(即旧项仍然存在,但其值已更改).如果这种理解是正确的,那么就会提出一个问题:如何使用可扩散数据源来实现以下方案?

So my current understanding is diffable data source uses item's hash for detecting item order change (i.e., new item inserted, an old item still existed, etc.), instead of item value change (i.e., an old item still existed but its value changed). If that understanding is correct, it then begs this question: how to use diffable data source to implement the following scenario?

  • 一个项目具有多个属性. UI中显示了一个属性(我们称其为属性A),但未用于生成哈希.
  • 该项目同时存在于旧快照和新快照中,但是其属性A发生了变化.因此,其用户界面需要更新.

在旧的UITableView API中,可以通过调用reloadRows()reloadData()来实现.但是如何使用可扩散的数据源呢?

In the old UITableView API, this can be implemented by calling reloadRows() or reloadData(). But how to do it using diffable data source?

更新:

花时间做实验并解决问题后,我相信上述问题的理解是错误的.请在下面查看我的答案.我相信这可以解释可变数据源的工作方式.我希望它能对也会有同样困惑的其他人有所帮助.我很高兴被证明是错误的.真的.因此,如果您有不同的看法,请留下您的答案.

After spending time doing experiments and solving the issue, I believe the understanding in above question was incorrect. Please see my answer below. I believe that explains how diffable data source works. I hope it helps to others who'll have the same confusion. I'd be glad to be proved wrong. Really. So please leave your answer if you think differently.

推荐答案

经过将近一天的无能为力的实验,我相信我已经弄清了可变数据源是如何工作的,并基于这种理解解决了我的问题(原来我的想法是几乎正确).

After almost one day's clueless experiments, I believe I figured out how diffable data source worked and solved my issue based on that understanding (it turned out my original thought was almost correct).

可扩散数据源使用项目哈希来标识项目. 对于旧快照和新快照中都存在的同一项目,可扩散数据源通过对旧值和新值执行"=="操作来检查该项目是否更改.

Diffable data source uses item hash to identify item. For the same item that exists in both old and new snapshots, diffable data source checks if the item changes by doing an "==" operation with its old and new values.

一旦弄清楚,它看起来是非常明显且简单的方法.但这太基础了,我不明白为什么在任何地方都没有明确提到它.

Once figured out, it looks like quite obvious and simple approach. But it's so fundamental that I can't understand why it isn't mentioned explicitly anywhere.

因此,要回答我的原始问题,是的,可扩散数据源可以检测项目值的变化.就是说,当项目值是引用类型和/或行中显示的文本是该对象引用的对象的属性(例如,Core Data中的关系)等时,这变得很棘手.

So, to answer my original question, yes, diffable data source can detect item value change. That said, it becomes tricky when item value is of reference type and/or the text shown in row is, say, properties of objects referenced by that object (e.g., relationship in Core Data), etc.

另一个说明.只要使用整个项目结构还是仅使用它的一部分来生成项目散列都没有关系,只要它可以识别项目即可.我更喜欢只使用项目的本质部分来真正识别它.

Another note. Whether using entire item struct or just part of it to generate item hash doesn't matter, as long as it identifies the item. I prefer to using only the essential part of the item which really identifies it.

这篇关于UITableViewDiffableDataSource可以检测到更改的项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆