簡體   English   中英

刪除UICollectionView中的最后一個單元會導致崩潰

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

嗨,我正在使用自定義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");

    }
}

因此,首先我要從數據源中刪除項目,然后從集合視圖中刪除項目。 這里是從數據源中刪除的代碼。

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

}

當我嘗試刪除最后一個對象,而所有其他對象都正確刪除時,為什么會發生這種情況? 我想了解這部分,以及如何解決這個問題? 謝謝大家。

我找到了解決方案! 我總是為節返回1,因此即使沒有任何項目,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];
    }

所以問題解決了!

我已經完成了此功能,在這里我為您提供了一個簡單的代碼,可以從“集合視圖”中刪除單元格。

只需在此處為您傳遞整數索引。

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