NSFetchedResultsContollerDelegate for CollectionView(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
- http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview
如果您正在寻找如何绕过NSInternalInconsistencyException 运行时异常UICollectionView,我在 GitHub 上有一个示例,详细说明了如何排队从 NSFetchedResultsControllerDelegate 更新.
If you're looking for how to get around the
NSInternalInconsistencyExceptionruntime exception withUICollectionView, I have an example on GitHub detailing how to queue updates from the NSFetchedResultsControllerDelegate.
问题是现有的 UITableView 类使用 beginUpdates和 endUpdates 将批次提交到表格视图.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:
- https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController
来自自述文件:
这是一个如何使用新的 UICollectionView 的示例NSFetchedResultsController.诀窍是将所做的更新排队通过 NSFetchedResultsControllerDelegate 直到控制器完成它的更新.UICollectionView 不一样beginUpdates 和 endUpdates UITableView 必须让它轻松工作使用 NSFetchedResultsController,所以你必须将它们排队,否则你会得到内部一致性运行时异常.
This is an example of how to use the new
UICollectionViewwithNSFetchedResultsController. The trick is to queue the updates made through theNSFetchedResultsControllerDelegateuntil the controller finishes its updates.UICollectionViewdoesn't have the samebeginUpdatesandendUpdatesthatUITableViewhas to let it work easily withNSFetchedResultsController, so you have to queue them or you get internal consistency runtime exceptions.
这篇关于CollectionView 的 NSFetchedResultsContollerDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CollectionView 的 NSFetchedResultsContollerDelegate
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- UITextView 内容插图 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
