一群程序猿的秘密基地iOS开发iOS精華

CollectionView Header的使用

2015-08-05  本文已影响31406人  David_Cap

CollectionView Header的添加和设置

建议先看CollectionView系列第一篇:http://www.jianshu.com/p/a1614404ae96

注册Header子类

这里不再赘述一些第一篇见过的东西,只介绍一下和添加 Cell不同的地方。

    //这里的HeaderCRView 是自定义的header类型
    [_muneView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Identifier];

自定义Header子类

自定义的话,header子类要继承自 UICollectionReusableView。然后在自行发挥。

要实现的代理方法

    //这个方法是返回 Header的大小 size
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
    
    //这个也是最重要的方法 获取Header的 方法。
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        
        NSString *CellIdentifier = @"header";
        //从缓存中获取 Headercell
        HeaderCRView *cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];
        return cell;
    }

当然 footer和header一样

上一篇下一篇

猜你喜欢

热点阅读