UICollectionView的HeaderView、FooterView

1。相关方法

      - (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier  //注册xib,以便后续重用

      - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath   //具体配置内容  --- <UICollectionViewDataSource>

      - (id)dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath  //重用


e.g. 自定义HeaderView、FooterView

       创建xib,继承UICollectionReusableView

       @interface FooterView : UICollectionReusableView
               @property (weak, nonatomic) IBOutlet 。。。;  //各控件声明
       @end  

       ============

       - (UICollectionViewFlowLayout *) flowLayout{
              UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
              flowLayout.。。。。。。//各属性设置
              flowLayout.headerReferenceSize = CGSizeMake(300.0f, 50.0f);  //设置head大小
              flowLayout.footerReferenceSize = CGSizeMake(300.0f, 50.0f);
              return flowLayout;
      }    

      UICollectionViewController *viewController =   [[UICollectionViewController  alloc]  initWithCollectionViewLayout:[self flowLayout]];

     ================

    static NSString *kCollectionViewCellIdentifier = @"CellId"; 

    static NSString *kCollectionViewHeaderIdentifier = @"HeaderId"; 

    static NSString *kCollectionViewFooterIdentifier = @"FooterId"; 

   - (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
              self = [super initWithCollectionViewLayout:layout];
              if (self != nil){
                     UINib *nib = [UINib nibWithNibName: NSStringFromClass([MyCollectionViewCell class]) bundle:[NSBundle mainBundle]];
                      [self.collectionView registerNib:nib  forCellWithReuseIdentifier:kCollectionViewCellIdentifier];  //注册加载单元格
 
                      UINib *headerNib = [UINib nibWithNibName:NSStringFromClass([HeaderView class])  bundle:[NSBundle mainBundle]];
                      [self.collectionView registerNib:headerNib  forSupplementaryViewOfKind :UICollectionElementKindSectionHeader  withReuseIdentifier: kCollectionViewHeaderIdentifier ];  //注册加载头
        
                      UINib *footerNib = [UINib   nibWithNibName:NSStringFromClass([FooterView class]) bundle:[NSBundle mainBundle]];
                      [self.collectionView registerNib:footerNib    forSupplementaryViewOfKind: UICollectionElementKindSectionFooter  withReuseIdentifier: kCollectionViewFooterIdentifier ];
             }
             return self;
    }

     - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath{
               MyCollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier  forIndexPath:indexPath];
               cell. 。。。。。
              return cell;
    }

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView   viewForSupplementaryElementOfKind:(NSString *)kind  atIndexPath:(NSIndexPath *)indexPath{
            NSString *reuseIdentifier = kCollectionViewHeaderIdentifier;
            if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
                    reuseIdentifier = kCollectionViewFooterIdentifier;
            }
    
           UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
          if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
                 HeaderView *header = (HeaderView *)view;
                 header.label.text = [NSString stringWithFormat:@"Section Header %lu",  (unsigned long)indexPath.section + 1];
         }
         else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
                FooterView *footer = (FooterView *)view;
                NSString *title = [NSString stringWithFormat:@"Section Footer %lu",  (unsigned long)indexPath.section + 1];
                [footer.button setTitle:title forState:UIControlStateNormal];
         }
          return view;
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值