CollectionView Header的使用

 collectionView的使用和tableView类似,遵守数据源和代理方法是必要的,而不太常用的是 CollectionView Header,如果你想给每一组section设置不同的cell也是也可行的

遵守下面的代理方法:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (self.attentionDataArr.count == 0) {
            PlaceholderView *placeHeader = (PlaceholderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:PlaceheaderIdentifier forIndexPath:indexPath];
            if (self.hasAttentionPeople == 1) {
                placeHeader.placeholdLabel.text = NSLocalizedString(@"HOME_ATTATENT_PLACEHOLDER_NULLLIVE_TIP", nil);
            }else if (self.hasAttentionPeople == 2) {
                placeHeader.placeholdLabel.text = NSLocalizedString(@"HOME_ATTATENT_PLACEHOLDER_NULLFOLLOW_TIP", nil);
            }
            return placeHeader;
        }else{
            return nil;
        }
    }else{
        AttenttionCellHeaderView *attentionHeaderView  = (AttenttionCellHeaderView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
        return attentionHeaderView;
    }
    
}

每个headerView的高度,用以下方法返回

//这个方法是返回 Header的大小 size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{

        if (section == 0) {
            if (self.attentionDataArr.count == 0) {
                return CGSizeMake(kScreenWidth, 429 * ScreenHeightRatio);
            }
            return CGSizeZero;
        }else{
            return CGSizeMake(kScreenWidth, self.headerH);
        }
    
}


就可以了,使用什么headerView记得要事先注册

[self.myCollectionView registerClass:[AttenttionCellHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier]; 


[self.myCollectionView registerClass:[PlaceholderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:PlaceheaderIdentifier];




 footer和header用法是一样的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值