NSFetchedResultsControllerDelegate 包装用于 UICollectionView 动画变化
只是
pod 'ABCollectionViewFRC'
并替换此代码(以 MagicalRecord 为例)
- (NSFetchedResultsController *)frc
{
if (_frc == nil) {
_frc = [MenuItem MR_fetchAllSortedBy:@"item_id" ascending:YES withPredicate:
[NSPredicate predicateWithFormat:@"section = %@",self.menuSection] groupBy:nil delegate:self];
}
return _frc;
}
用此代码替换
@property (nonatomic, strong) id<NSFetchedResultsControllerDelegate> delegateWrapper;
...
- (id<NSFetchedResultsControllerDelegate>)delegateWrapper
{
if (_delegateWrapper == nil)
_delegateWrapper = [[ABCollectionViewFRC alloc] initWithCollectionView:self.collectionView delegate:self];
return _delegateWrapper;
}
- (NSFetchedResultsController *)frc
{
if (_frc == nil) {
_frc = [MenuItem MR_fetchAllSortedBy:@"item_id" ascending:YES withPredicate:
[NSPredicate predicateWithFormat:@"section = %@",self.menuSection] groupBy:nil delegate:self.delegateWrapper];
}
return _frc;
}
您只需将代理设置为 ABCollectionViewFRC
而不是仅为 self
,然后您将能够动画化 UICollectionView
变化
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.collectionView deleteItemsAtIndexPaths:@[newIndexPath]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
break;
case NSFetchedResultsChangeMove:
[self.collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
break;
default:
break;
}
}
请随意讨论、拉取请求和 推文