CollectionView的NSFetchedResultsContollerDelegate [英] NSFetchedResultsContollerDelegate for CollectionView
问题描述
我想在CollectionViewController中使用NSFetchedResultsControllerRelegate。
因此,我只是改变了TableViewController的CollectionView的方法。
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
(
endUpdates
>。
But I do not know how to handle the WillChangeContent
(beginUpdates
for TableView
) and DidChangeContent
(endUpdates
for TableVie
w) 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
NSInternalInconsistencyException
runtime 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
从README:
这是一个如何使用新的
UICollectionView
和
NSFetchedResultsController
。诀窍是通过
NSFetchedResultsControllerDelegate
将
的更新排队,直到控制器
完成它的更新。UICollectionView
没有相同的
beginUpdates
和endUpdates
UITableView
必须让它工作轻松
与NSFetchedResultsController
,所以你必须排队他们或者你得到
内部一致性运行时异常。
This is an example of how to use the new
UICollectionView
withNSFetchedResultsController
. The trick is to queue the updates made through theNSFetchedResultsControllerDelegate
until the controller finishes its updates.UICollectionView
doesn't have the samebeginUpdates
andendUpdates
thatUITableView
has to let it work easily withNSFetchedResultsController
, so you have to queue them or you get internal consistency runtime exceptions.
这篇关于CollectionView的NSFetchedResultsContollerDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!