美文网首页
添加UICollectionView的headerView

添加UICollectionView的headerView

作者: FallPine | 来源:发表于2016-11-30 16:59 被阅读595次

使用UICollectionReusableView类来创建一个视图
然后在调用之前,和cell一样也是需要先注册的

- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

注册完后就可以在UICollectionViewController中调用下面的代理方法来创建了

/**
 *  使用headerView或footerView
 *  @para kind 判断是headerView还是footerView:UICollectionElementKindSectionHeader:headerView 
                                             UICollectionElementKindSectionFooter:footerView
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        QSPersonHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"PersonHeaderView" forIndexPath:indexPath];
        
        return headerView;
    }
    return nil;
}

相关文章

网友评论

      本文标题:添加UICollectionView的headerView

      本文链接:https://www.haomeiwen.com/subject/kyetmttx.html