After calling -[UICollectionView reloadData]
it takes some time for cells to be displayed, so selecting an item immediately after calling reloadData
do
create a method that does the selection and call it using performSelector after calling reload e.g;
[self performSelector:@selector(selectIt) withObject:self afterDelay:0.1];
I'm handling selection of cells in collectionView: cellForItemAtIndexPath:
. The problem I found was that if the cell didn't exist, simply calling selectItemAtIndexPath: animated: scrollPosition:
wouldn't actually select the item.
Instead you have to do:
cell.selected = YES;
[m_collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
Apple says:
You should not call this method in the middle of animation blocks where items are being inserted or deleted. Insertions and deletions automatically cause the table’s data to be updated appropriately.
In fact, you should not call this method in the middle of any animation (including UICollectionView
in the scrolling).
So, you can use:
[self.collectionView setContentOffset:CGPointZero animated:NO];
[self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
or mark sure not any animation, and then call reloadData
;
or
[self.collectionView performBatchUpdates:^{
//insert, delete, reload, or move operations
} completion:nil];
Hope this is helpful to you.