Given a section index, how can I get the UICollectionReusableView
for the section header?
Or how can I get the section index from the UICollectionReusableView
object.
I read this: How to get the index path of a UICollectionView header?, and similar posts but it doesn't work for me.
My sections are dynamic controlled by a NSFetchedResultsController
and there are multiple inserts, deletion and re-inserts. I suspect that the indices I store in collectionView:viewForSupplementaryElementOfKind:atIndexPath
and collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
are invalidated at some point.
Eventually I found a solution. Ugly but it seems to work:
- (SUGalleryHeaderView *)headerForSection:(NSInteger)section
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
NSString *kind = UICollectionElementKindSectionHeader;
UICollectionViewLayoutAttributes *attr =
[self.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
for (SUGalleryHeaderView *header in headersActive) {
if (CGRectEqualToRect(header.frame, attr.frame)) {
return header;
}
}
return nil;
}
Originally posted here: Getting supplementary view at specified index in UICollectionView