2

I have a UICollectionView that I want to reload right before it is shown. The number of items in the collection view's source may have changed so the reloading happens to add the new items or remove those that should no longer be there.

In my view controller I have:

  public override void ViewWillAppear(bool animated)
  {
     base.ViewWillAppear(animated);

     this.source.UpdateAvailableItems();
     this.CollectionView.ReloadSections(new NSIndexSet(0));
  }

This does what I want except that if items are getting removed, the collection view appears and the user briefly sees those items disappear. The situation is similar for adding new items, but it's a little harder to see the new items getting added.

(ReloadData basically doesn't work, which is why I'm using ReloadSections. See: UICollectionView reloadData not functioning properly in iOS 7)

(I can't really reload the collection view every time the source's items change, because that is happening potentially a hundred times per second when it's not visible)

Does anyone know of a way I could have the collection view reloaded and rendered completely before it appears? Wrapping the ReloadSections call with UIView.PerformWithoutAnimation also does not help.

ANSWER: C# implementation of Tim Edward's solution:

  public override void ViewWillAppear(bool animated)
  {
     base.ViewWillAppear(animated);

     this.source.UpdateAvailableItems();

     UIView.PerformWithoutAnimation(() =>
     {
        this.CollectionView.PerformBatchUpdates(
           () => this.CollectionView.ReloadSections(new NSIndexSet(0)),
           null);
     });
  }
Community
  • 1
  • 1
Aurast
  • 1,106
  • 9
  • 16
  • 1
    Just an idea, but have you tried putting base.ViewWillAppear at the end of your override, not at the start? – pbasdf Oct 01 '14 at 15:32
  • That's an excellent suggestion and I hadn't tried that but sadly I just did and it did not work. – Aurast Oct 01 '14 at 15:58

1 Answers1

3

Have you tried something like?

[UIView setAnimationsEnabled:NO];

[collectionView performBatchUpdates:^{

    // Reload sections here    

} completion:^(BOOL finished) {
    [UIView setAnimationsEnabled:YES];
}];
Tim Edwards
  • 261
  • 5
  • 8