UICollectionView 动画数据更改 [英] UICollectionView animate data change
问题描述
在我的项目中,我使用 UICollectionView 来显示图标网格.
In my Project I use UICollectionView to display a grid of icons.
用户可以通过单击一个分段控件来更改排序,该控件调用具有不同 NSSortDescriptor 的核心数据的提取.
The user is able to change the ordering by clicking a segmented control which calling a fetch from core data with different NSSortDescriptor.
数据量始终相同,只是在不同的部分/行中结束:
- (IBAction)sortSegmentedControlChanged:(id)sender {
_fetchedResultsController = nil;
_fetchedResultsController = [self newFetchResultsControllerForSort];
NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[self.collectionView reloadData];
}
问题是 reloadData 不会为更改设置动画,UICollectionView 只是弹出新数据.
The problem is that reloadData doesn't animate the change, UICollectionView just pops with the new data.
我应该跟踪一个单元格在更改之前和之后的 indexPath 中,并使用 [self.collectionView moveItemAtIndexPath: toIndexPath:] 来执行更改的动画还是有更好的方法?
Should I keep track in which indexPath a cell was before and after change, and use [self.collectionView moveItemAtIndexPath: toIndexPath:] to perform the animation for the change or there is a better method ?
我对子类化 collectionViews 没有太多了解,所以任何帮助都会很棒......
I didn't get much into subclassing collectionViews so any help will be great...
谢谢,比尔.
推荐答案
reloadData 不设置动画,在放入 UIView 动画块时也不可靠.它想在 UICollecitonView performBatchUpdates 块中,所以尝试更像:
reloadData doesn't animate, nor does it reliabably do so when put in a UIView animation block. It wants to be in a UICollecitonView performBatchUpdates block, so try something more like:
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:^(BOOL finished) {
// do something on completion
}];
这篇关于UICollectionView 动画数据更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!