关于UICollectionViewController的header和foot视图

随便商城项目的慢慢推进,以前慢慢生疏的控件又必须要再一次拾起,今天遇到的问题是怎么给collectionViewController添加一个label

在网上找了许多的方法,其中很多基本上都是说在storyBoard中先拉出header和foot的视图,然后在到

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 这个方法中来进行配置,(网上方法很多,这个就不说了)

现在就来说一说纯代码该怎么样进行配置呢?当然,核心api还是上面这个,但是需要在前期配置一下

1.创建继承自UICollectionReusableView的类.h

@interface LIUHeaderCollectionReusableView : UICollectionReusableView

@property(nonatomic,strong)UILabel *label;

@end

.m 发现register 中得dequeue是实现这个方法

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        [self addSubview:self.label];

    }

    return self;

}

2.在collection中注册

//注册headerView

    [self registerClass:[LIUHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    [self registerClass:[LIUHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];

 3.在上面所说的api中dequeue

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    

    NSString *reuseIdentifier;

    if ([kind isEqualToString: UICollectionElementKindSectionFooter]){

        reuseIdentifier = @"foot";

    }else{

        reuseIdentifier = @"header";

    }

        LIUHeaderCollectionReusableView *view =  (LIUHeaderCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

        //view.backgroundColor = [UIColor greenColor];

        //1.拿到section对应的字典

        NSString *text = self.allKeys[indexPath.section];

    view.label.text = text;

    }

    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){

        view.backgroundColor = [UIColor lightGrayColor];

        view.label.text = [NSString stringWithFormat:@"这是footer:%ld",(long)indexPath.section];

    }

    return view;

 }

 

4.就是要回答header和foot的size,要不是不会显示的,如 我这个是不要foot的

//foot不要

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {

//    return CGSizeMake(self.bounds.size.width, 30);

//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(self.bounds.size.width, 30);

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值