6

First things first:

  1. I do NOT Want to reload whole CollectionView.
  2. I also do NOT want to reload the section either (since it is same as reloadData because my cv only has 1 section).

I put some controls in the Supplementary View since this view acts as a header view. On some case, I want to hide/show the controls as needed. In order to do that I need to reload the Supplementary View as the data for it is already updated.

What I have tried:

UICollectionViewLayoutInvalidationContext *layoutContext =
[[UICollectionViewLayoutInvalidationContext alloc] init];
[layoutContext invalidateSupplementaryElementsOfKind:UICollectionElementKindSectionHeader
                                        atIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
[[_collectionView collectionViewLayout] invalidateLayoutWithContext:layoutContext];

This crash of course. The code doesn't look right either but I am not sure how to construct the UICollectionViewLayoutInvalidationContext properly and telling the collectionview to reload just the Supplementary View.

Thanks.

GeneCode
  • 7,545
  • 8
  • 50
  • 85

2 Answers2

6

If you need only to update the header view of a particular section, then you can do something similar to this:

if let header = collectionView.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: indexPath.section)) as? MyCollectionHeaderView {
    // Do your stuff here
    header.myLabel.isHidden = true // or whatever
}

Code fragment IndexPath(item: 0, section: indexPath.section) may seem a bit weird, but it turns out that the supplementaryView is only returned on the first item in the section.

Ely
  • 8,259
  • 1
  • 54
  • 67
  • Weird how you're forced to refactor out all setup code from the data source methods when you want to do this. – Andreas Jul 13 '20 at 14:41
  • 1
    After searching for the answer for over 2 days this is the one that finally works for Swift 5 on Xcode 11. – BVB09 Sep 20 '20 at 14:47
-1

I am not sure how you have grouped the cells of the collection view.

The simplest solution would be to reload the particular cell using:

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths
diOS
  • 39
  • 7
  • 1
    This only reload the normal cell. I want to reload the supplementaryview. Sorry i edited my question to indicate supplementaryview – GeneCode Feb 08 '17 at 09:26
  • In that case maybe you can iterate over all subviews in your uicollectionview and setNeedsDisplay only on the headerView. You can use the tag property to identify the headerView – diOS Feb 08 '17 at 10:36
  • 1
    setNeedsdisplay does not reload the view. it just refreshes it. – GeneCode Feb 08 '17 at 10:37
  • setNeedsDisplay calls for drawRect: method. It redraws the components again. – diOS Feb 08 '17 at 11:26
  • 1
    Yes it redraw the component based on the data called by viewForSupplementaryElementOfKind the first time. Thus, remains the same. And so does not do what I want. – GeneCode Feb 09 '17 at 06:48