UICollectionView 断言失败

61

UICollectionView中执行insertItemsAtIndexPaths时,我遇到了以下错误:

断言失败:

-[UICollectionViewData indexPathForItemAtGlobalIndex:], 
/SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:442
2012-09-26 18:12:34.432  
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'request for index path for global index 805306367 
when there are only 1 items in the collection view'

我已经检查过,我的数据源只包含一个元素。 有什么见解,为什么会发生这种情况?如果需要更多信息,我肯定可以提供。

14个回答

1
我也遇到了这个问题。以下是我的情况:
  1. 我子类化了UICollectionViewController,并在initWithCollectionViewLayout:上初始化了我的NSFetchedResultsController
  2. 从一个共享的类中获取NSURLConnection的结果并解析JSON字符串(不同的线程)
  3. 循环遍历feed并创建我的NSManagedObjects,将它们添加到我的NSManagedObjectContext中,该上下文具有主线程的NSManagedObjectContext作为parentContext
  4. 保存我的上下文。
  5. 让我的NSFetchedResultsController捕捉更改并排队。
  6. - (void)controllerDidChangeContent:上,我会处理更改并将其应用于我的UICollectionView

间歇性地,我会得到OP正在遇到的错误,但无法弄清楚原因。

为了解决这个问题,我将NSFetchedResultsController初始化和performFetch移动到我的- viewDidLoad方法中,现在这个问题已经解决了。不需要调用[collectionView reloadData]或其他任何操作,所有的动画都正常工作。

希望这可以帮到你!

0

我自己也遇到了这个问题。这里所有的答案似乎都存在问题,除了Alex L的回答。参考更新似乎是正确的答案。这是我的最终解决方案:

- (void)addItem:(id)item {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (!_data) {
            _data = [NSMutableArray arrayWithObject:item];
        } else {
            [_data addObject:item];
        }
        [_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:_data.count-1 inSection:0]]];
    }];
}

0
仅供参考,我遇到了同样的问题,我的解决方案是删除标题(在.xib中禁用它们),因为它们不再需要,所以删除了这个方法。之后一切都正常了。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementary
ElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接