UICollectionView: How to get the header view for a section?

前端 未结 4 1978
臣服心动
臣服心动 2021-01-31 09:15

There is a method to get a cell by indexPath (UICollectionView cellForItemAtIndexPath:). But I can\"t find a method to get one of the supplementary vie

相关标签:
4条回答
  • 2021-01-31 09:31

    First thing you have to do is check the box "Section Header" in the collection view's attribute inspector. Then add a collection reusable view just like you added your cell to the collection view, write an identifier and make a class for it if you need to. Then implement the method:

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    

    From there do exactly like you did with cellForItemAtIndexPath Its also important to specify if its a header or footer you are coding about:

    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        Header *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTitle" forIndexPath:indexPath];
        //modify your header
        return header;
    }
    
    else
    {
    
        EntrySelectionFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"entryFooter" forIndexPath:indexPath];
        //modify your footer
        return footer;
    }
    

    use indexpath.section to know what section this is in also note that Header and EntrySelectionFooter are custom subclasses of UICollectionReusableView that I made

    0 讨论(0)
  • 2021-01-31 09:38

    This method is often enough to serve the purpose of reloading on-screen supplementary views:

    collectionView.visibleSupplementaryViews(ofKind: UICollectionElementKindSectionHeader)
    
    0 讨论(0)
  • 2021-01-31 09:41

    I would like to share my insight of the solution provided by rob mayoff but I can't post comment so I am putting it here:

    For every one of you that tried to keep reference of the supplementary views being used by a collection view but who run into issues of loosing track too early because of

    collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
    

    being called too many times, try using an NSMapTable instead of a dictionary.

    I use

    @property (nonatomic, strong, readonly) NSMapTable *visibleCollectionReusableHeaderViews;
    

    created like this:

    _visibleCollectionReusableHeaderViews = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
    

    so that when you are keeping a reference to a supplementary view:

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        // ( ... )
        [_visibleCollectionReusableHeaderViews setObject:cell forKey:indexPath];
    

    it keeps only a WEAK reference to it in the NSMapTable and it keeps it AS LONG AS the object is not deallocated!

    You don't need anymore to remove the view from

    collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
    

    as the NSMapTable will lose the entry as soon as the view is deallocated.

    0 讨论(0)
  • 2021-01-31 09:50

    UPDATE

    As of iOS 9, you can use -[UICollectionView supplementaryViewForElementKind:atIndexPath:] to get a supplementary view by index path.

    ORIGINAL

    Your best bet is to make your own dictionary mapping index paths to supplementary views. In your collectionView:viewForSupplementaryElementOfKind:atIndexPath: method, put the view into the dictionary before returning it. In your collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:, remove the view from the dictionary.

    0 讨论(0)
提交回复
热议问题