4

I need to know if the UICollectionView header supplementary view is currently in view in order to force it to refresh/update when an app setting/preference is changed.

If the header is not in view, once scrolled to, the header will get drawn appropriately but right now that only happens on load and if the user scrolls off the header then back.

If the header is in view, I need it to update but I don't really want to have to reload the entire 0th section every time this setting changes (just when the header is in view...).

topwik
  • 3,487
  • 8
  • 41
  • 65

1 Answers1

6

You could do something like keep a property around when the view gets dequeued. Then change it when it disappears using:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

Depending on how you want your update to occur you could either keep a bool isVisible around or start an NSTimer when it appears and stop the timer when it disappears.

Unfortunately it doesn't look like there's a visibleSupplementaryViews method.

benuuu
  • 761
  • 2
  • 7
  • 24
  • hey, so I tried leveraging didEndDisplayingSuppView. Here's the problem: header view scrolls into view, or is currently in view. flag headerVisible = true. change setting that requires header update. call reload sections. viewForSuppElementOfKind sets headerVisible = true. then, for some reason, didEndDisplayingSuppView gets called and I set isVisible to false which leaves me in an incorrect state. the workflow always ends with didEndDisplaying so I can't seem to properly set the flag. – topwik Sep 17 '13 at 17:15
  • in my case when i have an empty collection view to start and viewForSuppView is called onload. Once I have data and reload the collection, didEndDisplaySupp is called. then when the data reloads, viewForSuppView is called again. which is fine. however, when reloading the section view when i think the header is in view, i call reloadSections, and viewForSuppView is called to dequeue the new header view, headerVisible= true set. then didEnd is getting called to end not the most recent, new viewForSuppView but the one before that... which is not expected. at least in my case. – topwik Sep 17 '13 at 17:53
  • You could keep a reference to the supplementary view when you create it. e.g. in viewForSupplementaryView do `if (indexPath.section == 0) { if (!self.myHeader) {self.myHeader = [collectionView dequeueSuppView]; } toReturn = self.myHeader; }` then perform the update with that property reference. Just make sure to nil out that reference at the right times because of how CV reuses views. – benuuu Sep 18 '13 at 21:11