I am making a collection view with all basic stuff, like default FlowLayout, unique section.
Assuming I'm correctly using collectionView:CellForItemAtIndexPath:
and all the other dataSource
protocols
I have mainData
, which is an array of dictionnaries (the original data), mainDataReferenceOrdered
, mainDataNameOrdered
and mainDataQuantiteOrdered
are other arrays containing the same data as mainData
(same elements pointed).
dataToDisplay
is the controller's current array pointer to ordered data.
When reordering, I just change data in collection view batch, like this:
[itemCollectionControl.collectionView performBatchUpdates:^{
dataToDisplay = mainDataReferenceOrdered; //or any other ordered array
[itemCollectionControl.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, itemCollectionControl.collectionView.numberOfSections)]];
} completion:^(BOOL finished) {}];
But the collection fades all cells, even if they are already visible, or at the right place.
I read the Apple documentation on UICollectionView, but I don't know what I missed. I also read other threads about that, but still looking for how I must do.
What do the batch looks to know which animation to apply on cells ?
My solution
This is the final code I use, certainly the closest of iOS programming guidelines I guess.
[itemCollectionControl.collectionView performBatchUpdates:^{
NSArray *oldOrder = dataToDisplay;
dataToDisplay = mainDataNBContainersOrdered;
for (NSInteger i = 0; i < oldOrder.count; i++) {
NSIndexPath *fromIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSInteger j = [dataToDisplay indexOfObject:oldOrder[i]];
NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:j inSection:0];
[itemCollectionControl.collectionView moveItemAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
}
} completion:^(BOOL finished) {
if (finished) {
[itemCollectionControl.collectionView reloadData];
}
}];
I simply browse all old ordered items, check their new position, and apply it with -[UICollectionView moveItemAtIndexPath:toIndexPath:]
There's still some duplicated cells glitches, but it looks fine on iOS7 (not on iOS6).
The completion might be useless on iOS7, I force the right order at the end on the iOS6 shuffling.
Edit
I think I found a solution, but I'm no longer able to test on that project. Maybe adding just 2 lines of code should fix this awful glitch.
before any call of -[UICollectionView moveItemAtIndexPath:toIndexPath:]
, call -[UICollectionView beginUpdates]
, and finally after all moves -[UICollectionView endUpdates]
.
If anyone able to test it found that it works, please tell me.