我正在尝试使用insertItemsAtIndexPaths将新项添加到我的集合视图中。我的应用程序在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:]
当单元格未在集合视图中注册时,会发生此断言。我正在注册我的手机。
答案 0 :(得分:1)
这对我有用: 如果Collection视图为空,则重新加载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];
}
}