Ios UICollectionView将项目移动到新分区
我在UICollectionView的动画方面遇到了严重的问题,类似于这里提到的问题: 假设我有一个UICollectionView,有5个不同的部分,每个部分有10个单元格。 我想用一个动画更新CollectionView,这样单元格将被重新排序为10个部分。不会添加包含新内容的单元格,也不会删除现有单元格 因此,我正在执行批量更新:Ios UICollectionView将项目移动到新分区,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我在UICollectionView的动画方面遇到了严重的问题,类似于这里提到的问题: 假设我有一个UICollectionView,有5个不同的部分,每个部分有10个单元格。 我想用一个动画更新CollectionView,这样单元格将被重新排序为10个部分。不会添加包含新内容的单元格,也不会删除现有单元格 因此,我正在执行批量更新: [_resultCollectionView performBatchUpdates:^{ [_resultCollectionView i
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertSections:insertSections];
[_resultCollectionView deleteSections:deleteSections];
//[_resultCollectionView insertItemsAtIndexPaths:insertIndexPaths];
//[_resultCollectionView deleteItemsAtIndexPaths:deleteIndexPaths];
for (all items which need to be moved...) {
[_resultCollectionView moveItemAtIndexPath:sourcePath toIndexPath:destinationPath];
}
} completion:^(BOOL finished) {
//[_resultCollectionView reloadData];
nil;
}];
如果执行insertSections、deleteSections、InsertItemSatineXPath、DeleteItemSatineXPath并在执行块后重新加载数据,则一切正常,这意味着我的数据源和委托在理论上工作正常。除了它还没有动画
如果我执行insertSections、deleteSections和moveItemAtIndexPath(这应该可以工作,因为单元格只会重新排列),我会遇到以下小错误:
无法将行移动到新插入的节(5)
如果执行insertSections、deleteSections和moveItemAtIndexPath,但排除新插入节中的任何移动,则显然会在节中获得无效的项目数
有人能解决这个问题吗 我遵循了john1034的链接,想出了一个相当棘手的方法。您必须将工作分为三个步骤:
static BOOL addingSections = NO;
static BOOL updatingCollectionView = NO;
static BOOL removingSections = NO;
- (void)reloadResultCollectionView
//IndexUnloadedArray: a link to my CollectionViewData before the update
//IndexArray: a link to my CollectionViewData after the update
//start only if something changed
if (![IndexArray isEqual:IndexUnloadedArray]) {
NSMutableIndexSet *deleteSections = [[NSMutableIndexSet alloc] init];
NSMutableIndexSet *insertSections = [[NSMutableIndexSet alloc] init];
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
//step 1 - add collectionView sections
for (int i = 0; i < IndexArray.count; i++) {
if (i >= IndexUnloadedArray.count) {
[insertSections addIndex:i];
}
}
NSLog(@"insert sections:%@", insertSections);
_sectionAmount = (int)_distanceUnloadedArray.count + (int)insertSections.count;
addingSections = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertSections:insertSections];
} completion:^(BOOL finished) {
nil;
}];
addingSections = NO;
//step 2 - update collectionView
//adding cells if there are not enough
for (int i = 0; i < IndexArray.count; i++) {
for (int j = 0; j < (int)[[IndexArray objectAtIndex:i] count]; j++) {
NSNumber *searchIndex = [[IndexArray objectAtIndex:i] objectAtIndex:j];
bool found = NO;
for (int k = 0; k < IndexUnloadedArray.count; k++) {
if ([[IndexUnloadedArray objectAtIndex:k] containsObject:searchIndex]) {
found = YES;
k = (int)IndexUnloadedArray.count;
}
}
if (!found) {
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
NSLog(@"insert cells:%@", insertIndexPaths);
//deleting cells if there are too many
for (int i = 0; i < IndexUnloadedArray.count; i++) {
if (![deleteSections containsIndex:i]) {
for (int j = 0; j < (int)[[IndexUnloadedArray objectAtIndex:i] count]; j++) {
NSNumber *searchIndex = [[IndexUnloadedArray objectAtIndex:i] objectAtIndex:j];
bool found = NO;
for (int k = 0; k < IndexArray.count; k++) {
if ([[IndexArray objectAtIndex:k] containsObject:searchIndex]) {
found = YES;
k = (int)IndexArray.count;
}
}
if (!found) {
[deleteIndexPaths addObject:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
}
NSLog(@"deleting cells:%@", deleteIndexPaths);
updatingCollectionView = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertItemsAtIndexPaths:insertIndexPaths];
[_resultCollectionView deleteItemsAtIndexPaths:deleteIndexPaths];
for (int i = 0; i < IndexUnloadedArray.count; i++) {
for (int j = 0; j < [[IndexUnloadedArray objectAtIndex:i] count]; j++) {
NSIndexPath *sourcePath = [NSIndexPath indexPathForRow:(j) inSection:i];
NSNumber *searchIndex = [[IndexUnloadedArray objectAtIndex:i] objectAtIndex:j];
NSIndexPath *destinationPath;
for (int k = 0; k < IndexArray.count; k++) {
if ([[IndexArray objectAtIndex:k] containsObject:searchIndex]) {
NSInteger *row = [[IndexArray objectAtIndex:k] indexOfObject:searchIndex];
destinationPath = [NSIndexPath indexPathForItem:row inSection:k];
if (sourcePath != destinationPath) {
NSLog(@"moving cell from %ld.%ld to %ld.%ld (%@)", (long)sourcePath.section, (long)sourcePath.row, (long)destinationPath.section, (long)destinationPath.row);
[_resultCollectionView moveItemAtIndexPath:sourcePath toIndexPath:destinationPath];
} else {
NSLog(@"object %ld.%ld stays in position", (long)sourcePath.section, (long)sourcePath.row);
}
}
}
}
}
} completion:^(BOOL finished) {
nil;
}];
updatingCollectionView = NO;
//step 3 - deleting sections if there are too many
for (int i = 0; i < IndexUnloadedArray.count; i++) {
if (i >= IndexArray.count) {
[deleteSections addIndex:i];
}
}
NSLog(@"delete sections:%@", deleteSections);
_sectionAmount = (int)_distanceArray.count;
removingSections = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView deleteSections:deleteSections];
} completion:^(BOOL finished) {
//update table header and footer
[_resultCollectionView reloadData];
}];
removingSections = NO;
}
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return _sectionAmount;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
if (addingSections) {
if (section < _distanceUnloadedArray.count) {
return [[_distanceUnloadedArray objectAtIndex:section] count];
} else {
return 0;
}
}
if (updatingCollectionView) {
if (section < _distanceArray.count) {
return [[_distanceArray objectAtIndex:section] count];
} else {
return 0;
}
}
if (removingSections) {
return [[_distanceArray objectAtIndex:section] count];
}
return [[_distanceArray objectAtIndex:section] count];
}
静态BOOL addingSections=NO;
静态BOOL updateingcollectionview=NO;
静态布尔删除部分=否;
-(void)重新加载ResultCollectionView
//IndexUnloadedArray:更新前指向我的CollectionViewData的链接
//IndexArray:更新后指向我的CollectionViewData的链接
//只有在发生变化时才开始
如果(![IndexArray isEqual:IndexUnloadedArray]){
NSMutableIndexSet*deleteSections=[[NSMutableIndexSet alloc]init];
NSMutableIndexSet*insertSections=[[NSMutableIndexSet alloc]init];
NSMutableArray*DeleteIndexPath=[[NSMutableArray alloc]init];
NSMutableArray*insertIndexPaths=[[NSMutableArray alloc]init];
//步骤1-添加collectionView节
对于(int i=0;i=INDEXUNLOADARAY.count){
[插入附件:i];
}
}
NSLog(@“插入节:%@”,插入节);
_sectionAmount=(int)\u distanceUnloadedArray.count+(int)insertSections.count;
添加部分=是;
[\u结果集合视图性能更新:^{
[_resultCollectionViewInsertSections:insertSections];
}完成:^(布尔完成){
无
}];
添加部分=否;
//步骤2-更新collectionView
//如果没有足够的单元格,则添加单元格
对于(int i=0;i