Reputation: 1071
I have an array of images that is displayed in a collectionView similar to Instagrams profile page. When an image is selected, the indexPath.row
gets passed to the next collectionView in which I use the same array and the passed indexPath.row
to know what image to display on the entire screen.
- (void)viewDidLayoutSubviews{
[self.view layoutIfNeeded];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedImage inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
The image comes up but I am unable to scroll horizontally between the rest of the images. Paging is enabled and each cell is large enough to take up the screen. If I comment out the above code, I am able to scroll through all the images. So pretty much how Twitter functions when you select a persons image and are able to scroll through the rest instead of x'ing out and selecting each one individually
Upvotes: 1
Views: 606
Reputation: 2881
I believe viewDidLayoutSubviews
is called whenever a collection view cell is reused. So when it tries reusing the cell, it runs the code that forces it to scroll back to that item, meaning that the scroll view never moves :) I think you need to make sure that scroll to the item only gets called once.
To test this out... try putting in a breakpoint in the code you have above, and see if this is getting called again as you try scrolling horizontally.
Upvotes: 2