删除UICollectionView中的最后一个单元格会导致崩溃
嗨我正在与一个自定义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"); } }
所以首先我从DataSource,然后从集合视图中删除项目。 这里是从数据源中删除的代码。
-(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]; }
为什么当我试图删除最后一个对象,当所有其他对象被正确删除时,这发生? 我想了解这部分,以及任何想法如何解决这个问题? 谢谢大家。
我find了解决scheme! 我总是返回1的部分,所以即使没有项目的CollectionView正在构build,并要求build立一个标题,像这样:
- (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]; }
所以问题解决了!
我已经完成了这个function,在这里我提供了一个简单的代码来从集合视图中删除单元格。
只要通过你在这里整数索引。
-(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 { } }
- 在View Controller之后,iOS块阻止exceptioncallback导致崩溃
- iOS7崩溃 – [__NSPlaceholderDictionary initWithObjects
- 如何在使用Objective C发生崩溃时获取行号,方法名称和类名称
- _GSRegisterPurpleNamedPortInPrivateNamespace – app store崩溃(附代码)
- 崩溃__pthread_kill
- 当用户通过语音input时,应用程序崩溃uitextview
- 请求用户在iPhone上崩溃后发送崩溃日志
- testing中的iPhone应用程序在几天后崩溃
- 应用程序被拒绝了两次,但不能在iOS 9.2上重现该问题。 我需要帮助确定它