So I am trying to write some code that scrolls a collection view to a certain index, then pulls a reference to the cell and does some logic. However I've noticed if that cell wasn't presently visible prior to the scroll, the cellForItemAtIndexPath
call will return nil, causing the rest of my logic to fail.
[_myView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index
inSection:0]
atScrollPosition:UICollectionViewScrollPositionTop
animated:NO];
//Tried with and without this line, thinking maybe this would trigger a redraw
[_myView reloadData];
//returns nil if cell was off-screen before scroll
UICollectionViewCell *cell =
[_myView cellForItemAtIndexPath:
[NSIndexPath indexPathForItem:index inSection:0]];
Is there some other method I have to call to cause the cellForItemAtIndexPath
to return something for a cell that suddenly came into view as a result of the scroll immediately preceding it?
I figured out a solution for now. If I call [myView layoutIfNeeded]
right after my call to reloadData, but before my attempt to retrieve the cell everything works fine. Right now all the cells are cached so access is fast, but I am scared this might give me bad performance if I have to load from the web or an internal database, but we'll see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With