如何在 UICollectionView performBatchUpdates 块中对移动、插入、删除和更新进行排序? [英] How to order moves, inserts, deletes, and updates in a UICollectionView performBatchUpdates block?

查看:28
本文介绍了如何在 UICollectionView performBatchUpdates 块中对移动、插入、删除和更新进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UICollectionView 中,我使用一个简单的自定义对象数组来生成和显示单元格.有时数据会发生变化,我想一次性为所有变化制作动画.为此,我选择跟踪第二个数组中的所有更改,将两者进行比较,并在 performBatchUpdates 块内生成一组移动、插入、删除和更新操作.我现在意识到在同一个块中完成所有这些非常棘手,因为您必须担心索引的操作顺序.事实上,对于这个问题的公认答案是错误(但在评论中更正).

In my UICollectionView, I use a simple array of custom objects to produce and display cells. Occasionally that data changes and I'd like to animate the changes all at once. I've chosen to do this by tracking all the changes in a second array, diff'ing the two, and producing a set of move, insert, delete, and update operations inside of a performBatchUpdates block. I now realize it's pretty tricky to do all of these inside the same block because you have to worry about orders of operations with indexes. In fact, the accepted answer to this issue is wrong (but corrected in the comments).

文档似乎相当缺乏,但它涵盖了一种情况:

The documentation seems pretty lacking, but it covers one case:

在批量操作中,删除是在插入之前处理的.这意味着删除的索引是相对于的索引进行处理的批量操作前集合视图的状态,以及插入的索引是相对于批量操作中所有删除后的状态.

Deletes are processed before inserts in batch operations. This means the indexes for the deletions are processed relative to the indexes of the collection view’s state before the batch operation, and the indexes for the insertions are processed relative to the indexes of the state after all the deletions in the batch operation.

但是,该文档没有讨论何时处理移动.如果我在同一个 performBatchUpdates 中调用 moveItemAtIndexPathdeleteItemsAtIndexPaths,移动索引应该是相对于删除前还是删除后的顺序?insertItemsAtIndexPaths怎么样?

However, the document doesn't talk about when moves are processed. If I call moveItemAtIndexPath and deleteItemsAtIndexPaths in the same performBatchUpdates, should the move indexes be relative to the pre- or post-deleted order? How about insertItemsAtIndexPaths?

最后,我在同一操作中遇到了调用 reloadItemsAtIndexPathsmoveItemAtIndexPath 的问题:

Finally, I'm facing issues calling reloadItemsAtIndexPaths and moveItemAtIndexPath in the same operation:

致命异常:NSInternalInconsistencyException 尝试删除并重新加载相同的索引路径

Fatal Exception: NSInternalInconsistencyException attempt to delete and reload the same index path

有没有办法在同一个 performBatchUpdates 中完成我想要的所有操作?如果是这样,更新相对于其他更新的处理顺序是什么?如果不是,人们通常会做什么?完成所有其他操作后重新加载数据?前?如果所有动画都发生在一个阶段,我会更喜欢.

Is there a way to do all the operations I want in the same performBatchUpdates? If so, what order do the updates get processed relative to the others? If not, what do people usually do? Reload the data after doing all other operations? Before? I'd much prefer if all the animations happened in a single stage.

推荐答案

对于移动操作,from indexPath 是删除前的索引,to indexPath 是删除后的索引.仅应为尚未插入、删除或移动的 indexPath 指定重新加载.这可能就是您看到 NSInternalInconsistencyException 的原因.

For the move operations, the from indexPath is pre-delete indices, and the to indexPath is post-delete indices. Reloads should only be specified for indexPaths that have not been inserted, deleted, or moved. This is probably why you're seeing the NSInternalInconsistencyException.

验证操作设置是否正确的简便方法:reload、insert 和 move-to indexPaths 集不应有任何重复,reload、delete 和 move-from indexPath 集不应有任何重复.

A handy way to verify the operations are set up correctly: the set of reload, insert and move-to indexPaths should not have any duplicates, and the set of reload, delete, and move-from indexPaths should not have any duplicates.

更新:

您移动的项目似乎没有更新,而只是移动了.因此,如果您需要更新和移动项目,您可以在批量更新之前或之后执行重新加载(取决于您的数据源的状态).

It appears that items that you move are not also updated, but only moved. So, if you need to update and move an item, you can perform the reload before or after the batch update (depending on the state of your data source).

这篇关于如何在 UICollectionView performBatchUpdates 块中对移动、插入、删除和更新进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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