After calling -[UICollectionView reloadData]
it takes some time for cells to be displayed, so selecting an item immediately after calling reloadData
do
I handled it on the willDisplayCell colelctionView delegate. The idea: A temp variable is needed to specify the initial scrolling has performed already or not (scrollIsRequired). When the last visible cell will display, than we can scroll to the required cell and set this variable to avoid scrolling again.
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
//Perform any configuration
if (CGRectGetMaxX(collectionView.frame) <= CGRectGetMaxX(cell.frame)) {
// Last visible cell
if (self.scrollIsRequired) {
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.initiallySelectedRepresentativeVerse inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
self.scrollIsRequired = NO;
}
}
}
It has worked for me like a charm.
Don't use reloadData
Use - (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion
instead. The completion block is executed after animations for cell insertion/deletion etc. have completed. You can put the call to reloadData in the (void (^)(void))updates
block
Make sure you're calling reloadData
on the main thread. That could be the cause for the delay in your cell updates.
Along the lines of this answer I found that calling layoutIfNeeded
after reloadData
seemed to effectively 'flush' the reload before I do other things to the collectionView:
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
...
On the page I found this solution, some commenters indicated it didn't work for them on iOS 9, but it's been fine for me so your mileage may vary.
The Swift way:
let selected = collectionView.indexPathsForSelectedItems()
collectionView.performBatchUpdates({ [weak self] in
self?.collectionView.reloadSections(NSIndexSet(index: 0))
}) { completed -> Void in
selected?.forEach { [weak self] indexPath in
self?.collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: [])
}
}
This is what worked for me:
I kept a reference of the selected index path and overide the reloadData function:
override func reloadData() {
super.reloadData()
self.selectItem(at: self.selectedIndexPath, animated: false, scrollPosition: UICollectionViewScrollPosition())
}
I tried doing it using indexPathForSelectedItems, but it was creating an infinite loop on collection view load.