ios - reloadItemsAtIndexPaths 上的 Collection View 崩溃

标签 ios objective-c uitableview uicollectionview

我有一个 uitableview,其中嵌套了一个 Collection View 。 Collectionview 和 Tableview 都只有 1 个部分。在 collectionview 单元格中有 uiimageviews。我正在从服务器下载图片,下载图片后,我想在 ImageView 中显示它们。我正在调用 reloadItemsAtIndexPaths 以重新加载单元格的内容以显示图片。这是我到目前为止发现的:

如果我正在使用

[NSIndexPath indexPathWithIndex:[secondArray indexOfObject:dictionaryToRefresh]]

作为在 reloadItemsAtIndexPaths 中刷新的索引,然后第一张图片在 Collection View 的第一个单元格中加载得很好。 reloadItemsAtIndexPaths 也有很好的淡入效果。然后它因错误而严重崩溃:

Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /SourceCache/UIKit/UIKit-3347.44/UICollectionView.m:3835 2015-05-03 18:47:02.385 FBomb[3117:798045] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete section 1, but there are only 1 sections before the update'

如果我用

[NSIndexPath indexPathForItem:[secondArray indexOfObject:dictionaryToRefresh] inSection:0];

所有图片都加载得很好!但是没有淡入效果,这就是我想要实现的。关于我可以克服这个问题的任何想法?我想显示带有淡入淡出效果的图片(我真的不明白为什么它没有发生)

提前致谢,请原谅打字错误!

编辑1 完整代码:

-(void) reloadItemWithDictionary:(NSDictionary*)dictionaryToRefresh{

        BOOL dictionaryFound = FALSE;
        NSIndexPath *= [[NSIndexPath alloc]init];
        NSIndexPath *indexPathSecondArray = [[NSIndexPath alloc] initWithIndex:0];

        for (NSDictionary *firstDictionary in megaArray)
        {
            NSArray *secondArray = [firstDictionary objectForKey:@"subArray"];
            if ([secondArray containsObject:dictionaryToRefresh]){
                indexPathFirstArray = [NSIndexPath indexPathForRow:[megaArray firstDictionary] inSection:0];

                //THIS FADES THE FIRST ITEM ONLY, THEN CREASHES
                //indexPathSecondArray = [NSIndexPath indexPathWithIndex:[secondoArray indexOfObject:dictionaryToRefresh]];
                //THESE WORKS, BUT THEY DON'T FADE
                indexPathSecondArray = [NSIndexPath indexPathForItem:[secondoArray indexOfObject:dictionaryToRefresh] inSection:0];
                //indexPathSecondArray = [NSIndexPath indexPathForRow:[secondoArray indexOfObject:dictionaryToRefresh] inSection:0];

                dictionaryFound = TRUE;
                break;
            }
        }

        if ( dictionaryFound == TRUE){

            dispatch_sync(dispatch_get_main_queue(), ^{

                UITableViewCell *tableCell = [tableWithCollectionView cellForRowAtIndexPath:indexPathFirstArray];
                UICollectionView *collectionViewInTableCell = (UICollectionView *)[tableCell viewWithTag:1000];

                    [collectionViewInTableCell performBatchUpdates:^{
                        [collectionView reloadItemsAtIndexPaths:@[indexPathSecondArray]];
                    } completion:^(BOOL finished){

                    }];
            });
        }   
}

最佳答案

当你尝试这样做时会发生什么:

NSUInteger x[] = {[secondArray indexOfObject:dictionaryToRefresh] , 0};
//I don't know if the first element is section or item. try both.
//NSUInteger x[] = {0, [secondArray indexOfObject:dictionaryToRefresh]};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:path]];

indexPathWithIndex 仅使用一个 NSInteger 创建实例,而 UITableViewUICollectionView 需要 item/的两个值行和节

关于ios - reloadItemsAtIndexPaths 上的 Collection View 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016719/

相关文章:

ios - 无论如何将 UITextfield 中的字符数限制为 5?

iOS8 : Blue bar "is Using Your Location" appears shortly after exiting app

ios - 如何在 UIKit 某处找到此崩溃的根源?

swift - 将空 uitableviewcells(占位符单元格)的左侧插入设置为 0 以消除左侧填充?

ios - 是否可以通过选择删除 UITableView 中的一行?

ios - 用 anchor 在uiwebview中加载本地html5

javascript - uiwebview javascript HTML 仅获取面向用户的正文

ios - Sprite Kit 中的简单碰撞

ios - 从数组中的数组访问对象(在模型类中定义)

使用加速度计(和陀螺仪)的 iOS 手势识别