简体   繁体   English

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

[英]Removing the last Cell in UICollectionView makes a Crash

Hi I'm working with a Custom UICollectionView ( https://github.com/SureCase/WaterfallCollectionView ) where everything works fine. 嗨,我正在使用自定义UICollectionView( https://github.com/SureCase/WaterfallCollectionView ),在此一切正常。 Now I'm setting up delete items from the UICollectionView, and I can delete them fine. 现在,我要设置UICollectionView中的删除项,然后就可以删除它们了。 The problem comes when I'm trying to delete the last item of the section. 当我尝试删除本节的最后一项时,问题就来了。

It gives the following error. 它给出以下错误。

*** 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}'

The Code I'm using to delete items is the following: 我用来删除项目的代码如下:

- (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");

    }
}

So first I'm removing items from DataSource and then from de Collection View. 因此,首先我要从数据源中删除项目,然后从集合视图中删除项目。 Here the Code for removing from Data Source. 这里是从数据源中删除的代码。

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

}

Why is this happening when I try to remove last object, when all other objects are removed correctly? 当我尝试删除最后一个对象,而所有其他对象都正确删除时,为什么会发生这种情况? I would like to understand this part, & any idea how to fix this? 我想了解这部分,以及如何解决这个问题? Thanks to all. 谢谢大家。

I Found a solution! 我找到了解决方案! I was always returning 1 for section, so even with no items the CollectionView was being constructed, and asking to build a Header, like this: 我总是为节返回1,因此即使没有任何项目,CollectionView仍在构建中,并要求构建Header,如下所示:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath; {

So I counted the items in my data array, and if the array is empty, there shouldn't be a section. 因此,我计算了数据数组中的项目,如果数组为空,则不应有任何部分。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    if(idObject.count>0){ return 1; }
    else{ return 0; }

}

So where I'm deleting the items from my data array, I can check if the array is empty, if is not empty I'll delete items, if it is, I'll delete the entire section. 因此,在要从数据数组中删除项目的地方,可以检查数组是否为空,如果不为空,则将删除项目,如果是,则将删除整个节。

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

So problem fixed! 所以问题解决了!

i have done this features, here i provides you a simple code to delete cell from Collection View. 我已经完成了此功能,在这里我为您提供了一个简单的代码,可以从“集合视图”中删除单元格。

Just pass you index in integer here. 只需在此处为您传递整数索引。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM