删除UICollectionView中的最后一个Cell会导致崩溃

时间:2014-06-27 08:14:28

标签: ios crash uicollectionview uicollectionviewlayout

您好我正在使用自定义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中删除项目,然后从de Collection View中删除项目。这里是从数据源中删除的代码。

-(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];

}

为什么在我尝试删除最后一个对象时,正确删除所有其他对象时会发生这种情况?我想了解这一部分,&amp;任何想法如何解决这个问题?谢谢大家。

2 个答案:

答案 0 :(得分:4)

我找到了解决方案! 我总是返回1为section,所以即使没有项目正在构建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];
    }

问题解决了!

答案 1 :(得分:2)

我已经完成了这些功能,在这里我为您提供了一个从Collection View中删除单元格的简单代码。

这里只需传递整数索引。

-(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 {

}    
}
相关问题