UICollectionView删除IndexPath时出现断言错误

6
这是我的错误信息:
*** Assertion failure in -[PSUICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionView.m:2801

我将以如下方式进行调用:

我这样调用:

[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:1]]];

为什么会发生这种情况,有什么想法吗?

你得到任何解决方案了吗? - CRDave
3个回答

7

您是否也从您的模型中删除了该项?例如,如果行数、section数和它们呈现的内容是从一个字典数组中获取的,其键表示section,每个数组表示行,则如果使用deleteItemsAtIndexPaths删除一行,则需要负责相应地更新字典。 UICollectionView不会为您执行此操作。


在我的情况下,在deleteItemAtIndexPath之前没有更新模型是导致错误的原因。 - marco alves
这个对我有用。应该标记为解决方案。 - stanley

5
请注意,您正在尝试从第1节删除索引1。索引和节都从0开始计数。
我是这样做的:
NSMutableArray *forms; // datasource item data for all the cells
int indexToDelete; // Set to the index you want to delete

...

[self.collectionView performBatchUpdates:^(void){
    [forms removeObjectAtIndex:indexToDelete]; // First delete the item from you model
    [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexToDelete inSection:0]]];
            } completion:nil];

1

在调用deleteItemsAtIndexPaths:方法时,请确保您的集合视图没有忙于其他操作。 我曾经遇到过使用insertItemsAtIndexPaths:方法时出现相同问题,后来发现是我的代码中存在一个错误 - 在调用[my collectionView reloadData]后立即调用了[myCollectionView insertItemsAtIndexPaths:]。因此,在调用insertItemsAtIndexPaths:时,我的集合视图正在重新加载其数据。修复了这个错误后,断言失败的问题就解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接