[英]Removing the last Cell in UICollectionView makes a Crash
嗨,我正在使用自定义UICollectionView( https://github.com/SureCase/WaterfallCollectionView ),在此一切正常。 现在,我要设置UICollectionView中的删除项,然后就可以删除它们了。 当我尝试删除本节的最后一项时,问题就来了。
它给出以下错误。
*** Assertion failure in -[UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UICollectionViewData.m:787
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionHeader at path <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}'
我用来删除项目的代码如下:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
NSLog(@"Erasing Objects!");
//First I remove Items from DataSource, and after from Collection View
[self deleteItemsFromDataSourceAtIndexPaths:selectedIndexes];
[self.cv deleteItemsAtIndexPaths:selectedIndexes];
[selectedIObjectsIndexes removeAllObjects];
[selectedIndexes removeAllObjects];
EditUICollection=NO;
EasyMediaGreenView.hidden = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.7 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.cv reloadData];
});
}else{
NSLog(@"Not delete");
}
}
因此,首先我要从数据源中删除项目,然后从集合视图中删除项目。 这里是从数据源中删除的代码。
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths
{
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *itemPath in itemPaths) {
[indexSet addIndex:itemPath.row];
}
[idObject removeObjectsAtIndexes:indexSet];
[typeObject removeObjectsAtIndexes:indexSet];
[urlObject removeObjectsAtIndexes:indexSet];
[textObject removeObjectsAtIndexes:indexSet];
}
当我尝试删除最后一个对象,而所有其他对象都正确删除时,为什么会发生这种情况? 我想了解这部分,以及如何解决这个问题? 谢谢大家。
我找到了解决方案! 我总是为节返回1,因此即使没有任何项目,CollectionView仍在构建中,并要求构建Header,如下所示:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath; {
因此,我计算了数据数组中的项目,如果数组为空,则不应有任何部分。
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if(idObject.count>0){ return 1; }
else{ return 0; }
}
因此,在要从数据数组中删除项目的地方,可以检查数组是否为空,如果不为空,则将删除项目,如果是,则将删除整个节。
NSLog(@"Erasing Objects!");
[self deleteItemsFromDataSourceAtIndexPaths:selectedIndexes];
if(idShadeInside.count > 0){
[self.cv deleteItemsAtIndexPaths:selectedIndexes];
}
else{
//Creating an IndexSet with the Section to Erase
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];[indexSet addIndex:0];
[self.cv deleteSections:indexSet];
}
所以问题解决了!
我已经完成了此功能,在这里我为您提供了一个简单的代码,可以从“集合视图”中删除单元格。
只需在此处为您传递整数索引。
-(void)remove:(int)i {
@try {
[self.collection performBatchUpdates:^{
[self.arrayThumbImg removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.collection deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
@catch (NSException *exception) {
NSLog(@"Error: %@",exception);
}
@finally {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.