I am writing an app which makes use of UICollectionView to display some content in the CollectionView Cells. With pinch gestures, the collection view needs to be reloaded. I am able to handle the change of content according to the pinch gestures. Now, on the pinch gesture START, the collection view needs to reloaded and scrolled to a specific index. To do that, I have called the functions like this:
[tempCollectionView reloadData];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollToItem) userInfo:nil repeats:YES];
[OR]
[self performSelector:@selector(scrollToItem) withObject:nil afterDelay:0.0];
- (void) scrollToItem
{
if (m_selectedCellIndex == -1)
{
NSLog(@"cell return");
return;
}
NSIndexPath *iPath = [NSIndexPath indexPathForItem:m_selectedCellIndex inSection:0];
[tempLocalFilesCollectionView scrollToItemAtIndexPath:iPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
LocalFilesCollectionViewCell *cell = (LocalFilesCollectionViewCell *) [m_tempLocalFilesCollectionView cellForItemAtIndexPath: iPath];
if (cell)
{
CGPoint p = [tempCollectionView convertPoint: cell.center fromView: tempCollectionView];
NSLog(@"center: %f, %f, %f, %f", cell.center.x, cell.center.y, p.x, p.y);
}
else
{
NSLog(@"Not found");
}
}
But the scroll doesn't work. It crashes with the error "attempt to scroll to invalid index path". It is obvious that the tempCollectionView doesn't have any cells ready to be scrolled.
How can I scroll to the index path after the reload is finished? Any help would be great!!