Refresh bug with UICollectionView

Viewed 1363

I have a UICollectionView that uses a UICollectionViewFlowLayout in conjunction with a UIRefreshControl. When I pull to refresh, the following block of code is called

self.content = nil;
self.content = [[[posts reverseObjectEnumerator] allObjects] copy];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];
    [self.refreshControl endRefreshing];
 });

What ends up happening is when the collectionview is pulled down to refresh, only cells at index 0 and 1 are visible. However, when the control is released, the cell at index 2 pops back into view and (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *) gets called to dequeue the cell at index 2. This happens BEFORE reloadData is called. So the order of execution is such

  1. Pull down control
  2. Cell at index 2 goes off screen
  3. Release
  4. Cell at index 2 comes back on screen
  5. (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *) is called on that cell
  6. [self.collectionView reloadData]; is called
  7. The cells at indexes 0, 1, and 2 all are dequeued in (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)

It's pretty subtle but there's a slight flicker as, for some godforsaken reason, the cell at index 0 gets set to the cell at index 2's content and then set back to the correct content. There's another problem though because my UICollectionViewSubclass has both a UITextView and UIImageView so even though the textview just flickers, the images at 0 and 2 get swapped and don't switch until I refresh again.

Anyone have any ideas how to stop 2 from getting reloaded before the called to reloadData happens? or any fix really

EDIT: I discovered that this doesn't necessarily happen once you release the refresh control, it's actually when the refresh control is pulled far enough to trigger the refresh and then it does a weird little jump/flicker on the screen. In this case, the lone cell got dequeued and then it resets correctly. This is only if I don't release the control

0 Answers
Related