iOS的UICollectionView添加header

实现效果
这里写图片描述

一、创建UICollectionView

#import "HomeViewController.h"  
#import "ConstomCell.h"  

static NSString *headerViewIdentifier = @"hederview";  

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegateUICollectionViewDelegateFlowLayout>  

@property (nonatomic,strong) UIImageView *headerImage;  
@end  

@implementation HomeViewController  

- (void)viewDidLoad {  
    [super viewDidLoad];  
    //1.添加collectionview  
    [self addCollectionView];   
}  

-(void)addCollectionView  
{  
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];  
    //设置每一行的间距  
   layout.minimumLineSpacing=20;  
    //设置每个单元格的大小   layout.itemSize=CGSizeMake(100, 100);   
    layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0);  
  //设置collectionView头视图的大小  layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); 

    UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];  
    collectionView.frame=self.view.bounds;   
   //注册cell单元格  
   [collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];  
    //注册头视图  
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];  
    collectionView.backgroundColor=[UIColor whiteColor];  
    collectionView.delegate=self;  
    collectionView.dataSource=self;  
    [self.view addSubview:collectionView];  
}  

二、代理方法

#pragma mark  返回多少行  
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
{  
    return 13;  
}  
#pragma markk 返回的单元格  
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
{  
    ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];  
    return cell;  
}  

//  返回头视图  
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath  
{  
    //如果是头视图  
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {  
         UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];  
        //添加头视图的内容  
        [self addContent];  
        //头视图添加view  
        [header addSubview:self.headerImage];  
        return header;  
    }  
    //如果底部视图  
//    if([kind isEqualToString:UICollectionElementKindSectionFooter]){  
//    }  
    return nil;  
}  

/* 
 *  补充头部内容 
 */  
-(void)addContent  
{  
    UIImageView *headerImage=[[UIImageView alloc]init];  
    headerImage.contentMode=UIViewContentModeScaleAspectFill;  
    headerImage.clipsToBounds=YES;  
    headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);  
    headerImage.image=[UIImage imageNamed:@"mei"];  
    self.headerImage=headerImage;  
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值