我正在我UICollectionView
的一个应用程序中进行试验。当我将一个项目添加到集合视图时,我收到一个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'must return a UICollectionViewLayoutAttributes instance from -layoutAttributesForItemAtIndexPath:
for path <NSIndexPath 0x10486f40> 2 indexes [0, 0]'
以下是我如何将项目添加到集合视图并更新它(在 中viewWillAppear:
):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.moc = [[AZStackifyDataModel shareDataModel] mainContext];
[AZUser getAllUsersUsingManagedObjectContext:self.moc
success:^(NSArray *users) {
[self.collectionView performBatchUpdates:^{
self.collectionData = [users mutableCopy];
NSMutableArray *indexPaths = [NSMutableArray array];
for (int i = 0; i < self.collectionData.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:nil];
}
failure:^(NSError *error) {
}];
}
如果我用这段代码替换调用performBatchUpdates:completion:
,一切正常:
self.collectionData = [users mutableCopy];
[self.collectionView reloadData];
请问有人知道我的代码有什么问题吗?
注意:我有 subclassed UICollectionViewCell
,但我不认为这是问题,因为collectionView:cellForItemAtIndexPath:
当应用程序崩溃时甚至没有调用/到达......
编辑 1:这是完整视图控制器和自定义集合视图单元:https ://gist.github.com/AzizLight/4396a9cfe10511e59988
如果您需要更多代码,请随时询问。