19

I have an uicollectionview with supplementaryView for each section. In each supplementaryView I have some buttons. In dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: I'm setting the buttons tags with the indexPath.section. I made some delegate methods when a button is pressed and send the section(button.tag) as parameter.

Everything works fine, but here comes the problem:

  • when I insert or delete a section it must update the buttons tags, but I don't want to reload all the items from each section.

I've tried:

  • to get the supplementaryViews with the dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: and call layoutIfNeeded method for each supplementaryView, in this way the supplementaryViews are reloading, but this approach multiply my supplementaryViews.
  • a fast for in collectionView.subviews and get just the supplementaryView and call layoutIfNeeded method, but it doesn't reload, just the layout is reloading.
  • reloadSections: method, but this reload all items from the section

Ohh.. And I use NSFetchedResultsController(delegate) for inserting and deleting sections.

Can anyone help me with this? Is there a way to reload only the supplementaryView? Is there other approach for this scenario?

Thank You!

Irina
  • 191
  • 1
  • 1
  • 4
  • I solved it. I use custom *UICollectionViewLayoutAttributes* for my *supplementaryView* and one of the custom attribute is a *NSInteger* that keeps the section and in the NSFetchedResultsController delegate method *controllerDidChangeContent:* after inserting or deleting sections, I invalidate the collection view layout. I'm not sure if is best practice, but it works and time is important right now :) – Irina Nov 18 '14 at 16:44

2 Answers2

12

To reload supplementary views you can use -invalidateLayoutWithContext:.

See documentation for UICollectionViewLayoutInvalidationContext and method -invalidateSupplementaryElementsOfKind:atIndexPaths: (available since iOS 8).

Marián Černý
  • 15,096
  • 4
  • 70
  • 83
  • [`invalidateSupplementaryElementsOfKind`](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewLayoutInvalidationContext_class/index.html#//apple_ref/occ/instm/UICollectionViewLayoutInvalidationContext/invalidateSupplementaryElementsOfKind:atIndexPaths:) is available on iOS 8 and higer. – testing Aug 10 '15 at 10:18
  • It was an old question, but good to know for future similar situations. Thanks! – Irina Oct 29 '15 at 16:42
  • 2
    Any idea how I can get the supplementary view reload to animate with invalidLayout? – yuf Sep 29 '16 at 22:05
  • 21
    This doesn't actually seem to trigger the `collectionView(_:viewForSupplementaryElementOfKind:)` API – Bob Spryn Aug 18 '17 at 15:13
8

Instead of reloading these views, getting a reference to each visible supplementary view so that you can change the tag might be sufficient for your needs.

There is a simple method you can use to do this:

let supplementaryViews = collectionView.visibleSupplementaryViews(ofKind: "identifier")

https://developer.apple.com/documentation/uikit/uicollectionview/1618026-visiblesupplementaryviews

Jordan Smith
  • 10,310
  • 7
  • 68
  • 114