添加UICollectionView的headerView

使用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)