xcode deleteItemsAtIndexPaths 中的 UICollectionView 断言失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13707181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView Assertion failure in deleteItemsAtIndexPaths
提问by Devfly
Here is my error:
这是我的错误:
*** Assertion failure in -[PSUICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionView.m:2801
I'm calling it like this:
我这样称呼它:
[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:1]]];
Why is this happening, any ideas?
为什么会发生这种情况,有什么想法吗?
回答by Valentin Radu
Do you remove the item from your model as well? So, for example, if the number of rows, sections and the content they present is taken from a dictionary of arrays whose keys represent the sections and each array the rows, then if you delete one row with deleteItemsAtIndexPaths
you're responsible to update the dictionary accordingly. UICollectionView
will not do it for you.
您是否也从模型中删除了该项目?因此,例如,如果行数、节数和它们呈现的内容取自数组字典,其键代表节,每个数组代表行,那么如果您删除一行,则deleteItemsAtIndexPaths
您有责任更新字典因此。UICollectionView
不会为你做的。
回答by Peter Boné
Note that you are trying to delete index 1 from section 1. Both index and section starts at 0.
请注意,您正在尝试从第 1 部分中删除索引 1。索引和部分都从 0 开始。
i did it like this:
我是这样做的:
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];
回答by Aliaksei
Check that your collection view is not busy with something else when you call deleteItemsAtIndexPaths: . I experienced the same problem with insertItemsAtIndexPaths: method, and it turned out that it was caused by a bug in my code - I called [myCollectionView insertItemsAtIndexPaths:] immediately after calling [my collectionView reloadData] . So, at the moment of calling insertItemsAtIndexPaths: my collection view was reloading its data. After I fixed this bug, the problem with assertion failure has gone.
当您调用 deleteItemsAtIndexPaths: 时,检查您的集合视图是否忙于其他事情。我在使用 insertItemsAtIndexPaths: 方法时遇到了同样的问题,结果证明它是由我的代码中的错误引起的 - 我在调用 [my collectionView reloadData] 后立即调用了 [myCollectionView insertItemsAtIndexPaths:] 。因此,在调用 insertItemsAtIndexPaths 时:我的集合视图正在重新加载其数据。在我修复了这个错误之后,断言失败的问题就消失了。