Ios 删除UICollectionView中的最后一个单元格会导致崩溃

Ios 删除UICollectionView中的最后一个单元格会导致崩溃,ios,crash,uicollectionview,uicollectionviewlayout,Ios,Crash,Uicollectionview,Uicollectionviewlayout,大家好,我正在使用一个定制的UICollectionView(),它一切正常。现在我正在设置从UICollectionView删除项目,我可以很好地删除它们。当我试图删除该节的最后一项时,问题出现了 NSLog(@"Erasing Objects!"); [self deleteItemsFromDataSourceAtIndexPaths:selectedIndexes]; if(idShadeInside.count > 0){ [self.cv de

大家好,我正在使用一个定制的UICollectionView(),它一切正常。现在我正在设置从UICollectionView删除项目,我可以很好地删除它们。当我试图删除该节的最后一项时,问题出现了

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];
    }
它给出了以下错误

*** 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}'
所以首先我要从DataSource中删除项,然后从de Collection视图中删除项。下面是从数据源中删除的代码

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

}

当我尝试删除最后一个对象时,当所有其他对象都正确删除时,为什么会发生这种情况?我想了解这一部分,有没有办法解决这个问题?感谢大家。

我已经完成了这些功能,这里我为您提供了一个从集合视图中删除单元格的简单代码

只需在这里传递整数索引

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

}    
}
我找到了解决办法! 我总是为小节返回1,因此即使没有任何项目,CollectionView也在构建中,并要求构建一个标题,如下所示:

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

所以问题解决了

感谢您的回答,但问题不是删除项目。我所能做的就是删除集合视图的最后一项。***-[UICollectionViewData LayoutAttributes for Supplmentary Element of Kind:atIndexPath:]、/SourceCache/UIKit/UIKit-2935.137/UICollectionViewData.m:787***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序中的断言失败,原因:'no-UICollectionViewLayoutAttribute实例-LayoutAttributesForSupplmentalElementOfKind:UICollectionElementKindSectionHeader在路径{length=2,path=0-0}'处,然后在删除单元格调用[collectionview reload]后不久;方法。