iOS中UICollectionView添加头视图

参考链接:https://www.jianshu.com/p/ef57199bf34a

找了一堆的博客,写的都少了很重要的一步。

//引入头部视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    NSLog(@"kind = %@", kind);
    if (kind == UICollectionElementKindSectionHeader){
        
        CouponCollHeaderView *headerV = (CouponCollHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellHeadID forIndexPath:indexPath];
        headerV.backgroundColor = [UIColor greenColor];
        [headerV getSHCollectionReusableViewHearderTitle:@"这是标题"];
        reusableview = headerV;
    }
    
    return reusableview;
    
}
//设置头部高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(SCREEN_WIDTH, CONVER_VALUE(245.5f));
}

剩下的后期整理。

猜你喜欢

转载自www.cnblogs.com/liuzhi20101016/p/11909040.html