删除UICollectionView中的最后一个单元格会导致崩溃 [英] Removing the last Cell in UICollectionView makes a Crash

查看:3049
本文介绍了删除UICollectionView中的最后一个单元格会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个自定义UICollectionView( https://github.com/SureCase/WaterfallCollectionView)其中一切工作正常。现在我设置从UICollectionView删除项目,我可以删除它们。问题出现时,我试图删除该部分的最后一项。

Hi I'm working with a Custom UICollectionView (https://github.com/SureCase/WaterfallCollectionView) where everything works fine. Now I'm setting up delete items from the UICollectionView, and I can delete them fine. The problem comes when I'm trying to delete the last item of the section.

它会出现以下错误。

*** 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集合视图。这里是从数据源中删除的代码。

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.

推荐答案

我找到了一个解决方案!
我总是返回1的section,所以即使没有项目CollectionView正在构建,并要求建立一个头,像这样:

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:

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

所以问题已解决!

这篇关于删除UICollectionView中的最后一个单元格会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆