CollectionView 的 NSFetchedResultsContollerDelegate [英] NSFetchedResultsContollerDelegate for CollectionView

查看:25
本文介绍了CollectionView 的 NSFetchedResultsContollerDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 CollectionViewController 中使用 NSFetchedResultsControllerRelegate.因此,我只是为 CollectionView 更改了 TableViewController 的方法.

I'd like to use the NSFetchedResultsControllerRelegate in a CollectionViewController. Therefore I just changed the method for the TableViewController for the CollectionView.

(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
            break;

        case NSFetchedResultsChangeDelete:
            [self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] ];

       break;
    }
}


(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath {

  UICollectionView *collectionView = self.collectionView;

  switch(type) {

    case NSFetchedResultsChangeInsert:
        [collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]];
        break;

    case NSFetchedResultsChangeDelete:
        [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        break;

    case NSFetchedResultsChangeUpdate:
        [collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        break;

    case NSFetchedResultsChangeMove:
        [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        [collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]];
        break;
  }
}

(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
   [self.collectionView reloadData];
}

但我不知道如何处理 WillChangeContent (beginUpdates for TableView) 和 DidChangeContent (<TableView) 的 CollectionView 的 code>endUpdates.

But I do not know how to handle the WillChangeContent (beginUpdates for TableView) and DidChangeContent (endUpdates for TableView) for a CollectionView.

一切正常,除非我将一个项目从一个部分移动到另一个部分.然后我收到以下错误.

Everything works fine except when I move one item from one section to another section. Then I get the following error.

这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的错误.无效更新:第 0 节中的项目数无效....

This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. Invalid update: invalid number of items in section 0....

知道如何解决这个问题吗?

Any idea how can I solve this issue?

推荐答案

将获取的结果控制器与集合视图结合起来有点棘手.问题在

Combining a fetched results controller with a collection view is a bit tricky. The problem is explained in

如果您正在寻找如何绕过NSInternalInconsistencyException 运行时异常UICollectionView,我在 GitHub 上有一个示例,详细说明了如何排队从 NSFetchedResultsControllerDelegate 更新.

If you're looking for how to get around the NSInternalInconsistencyException runtime exception with UICollectionView, I have an example on GitHub detailing how to queue updates from the NSFetchedResultsControllerDelegate.

问题是现有的 UITableView 类使用 beginUpdatesendUpdates 将批次提交到表格视图.UICollectionView有一个新的 performBatchUpdates: 方法,它接受一个块参数更新集合视图.这很性感,但效果不佳使用 NSFetchedResultsController 的现有范例.

The problem is that the existing UITableView class uses beginUpdates and endUpdates to submit batches to the table view. UICollectionView has a new performBatchUpdates: method, which takes a block parameter to update the collection view. That's sexy, but it doesn't work well with the existing paradigm for NSFetchedResultsController.

幸运的是,那篇文章还提供了一个示例实现:

Fortunately, that article also provides a sample implementation:

来自自述文件:

这是一个如何使用新的 UICollectionView 的示例NSFetchedResultsController.诀窍是将所做的更新排队通过 NSFetchedResultsControllerDelegate 直到控制器完成它的更新.UICollectionView 不一样beginUpdatesendUpdates UITableView 必须让它轻松工作使用 NSFetchedResultsController,所以你必须将它们排队,否则你会得到内部一致性运行时异常.

This is an example of how to use the new UICollectionView with NSFetchedResultsController. The trick is to queue the updates made through the NSFetchedResultsControllerDelegate until the controller finishes its updates. UICollectionView doesn't have the same beginUpdates and endUpdates that UITableView has to let it work easily with NSFetchedResultsController, so you have to queue them or you get internal consistency runtime exceptions.

这篇关于CollectionView 的 NSFetchedResultsContollerDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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