Collectionview performBatchUpdates 崩溃 [英] Collectionview performBatchUpdates crash
问题描述
我正在尝试使用 insertItemsAtIndexPaths 将新项目添加到我的集合视图中.我的应用程序在 performBatchupdate 时崩溃
I am trying to add new items to my collection view using insertItemsAtIndexPaths. My app crashes at performBatchupdate
- (void) addItems {
NSArray *newProducts = @[@"1",@"2",@"3",@"4"];
[self.collectionView performBatchUpdates:^{
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) {
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
[self.array addObjectsFromArray:newProducts];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion:nil];
}
以下是崩溃日志:
* 断言失败 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
* Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
当单元格未注册到 collectionview 时,会发生此断言.我正在注册我的手机.
This Assertion happens when cell is not registered with the collectionview. I am registering my cell.
推荐答案
这对我有用:如果 Collection 视图为空,则重新加载其他 insertItems.
This worked for me: If Collection view is empty reload else insertItems.
- (void)addItems {
NSArray *newProducts = @[@"1",@"2",@"3",@"4"];
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) {
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
if (self.array) {
[self.array addObjectsFromArray:newProducts];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion:nil];
}
else {
self.array = [[NSMutableArray alloc] initWithArray:newProducts];
[self.collectionView reloadData];
}
}
这篇关于Collectionview performBatchUpdates 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!