转载:怎么给collection View 添加头视图

      经常有朋友在群里面问collectionView怎么添加头视图,不止一个问的,好多都在问,所以小编索性就写了一篇collectionView怎么添加头视图的博客,供大家参考学习。(示例代码下载地址)

    我们要使用CollectionView里面的头视图需要先注册头视图 UICollectionReusableView或者 继承UICollectionReusableView的子类,kind类型为UICollectionElementKindSectionHeader,并且需要带一个标识符,我们定义个static 的静态字符串就行,如下所示:

 [collectionView registerClass:[UICollectionReusableViewclass ]   forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];

几个重要的不能忽视的点就是  UICollectionViewFlowLayout 布局属性需要设置 headerReferenceSize头部的大小,不然头视图没有大小不显示;一定要在 

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; 方法里面创建头视图view并给出frame,然后添加到  UICollectionReusableView 中。

详细代码如下

[objc] view plain copy
  1. <pre name="code" class="objc">//  
  2. //  HomeViewController.m  
  3. //  collection添加头部  
  4. //  
  5. //  Created by user on 15/10/10.  
  6. //  Copyright (c) 2015年 user. All rights reserved.  
  7. //  
  8.   
  9. #import "HomeViewController.h"  
  10. #import "ConstomCell.h"  
  11.   
  12.   
  13. static NSString *headerViewIdentifier = @"hederview";  
  14.   
  15. @interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>  
  16.   
  17. @property (nonatomic,strong) UIImageView *headerImage;  
  18.   
  19. @end  
  20.   
  21. @implementation HomeViewController  
  22.   
  23. - (void)viewDidLoad {  
  24.     [super viewDidLoad];  
  25.     //1.添加collectionview  
  26.     [self addCollectionView];  
  27.     
  28. }  
  29.   
  30. -(void)addCollectionView  
  31. {  
  32.     UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];  
  33.     layout.minimumLineSpacing=20; //设置每一行的间距  
  34.     layout.itemSize=CGSizeMake(100, 100);  //设置每个单元格的大小  
  35.     layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0);  
  36.     layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); //设置collectionView头视图的大小  
  37.       
  38.     UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];  
  39.     collectionView.frame=self.view.bounds;  
  40.     //注册cell单元格  
  41.    [collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];  
  42.     //注册头视图  
  43.     [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];  
  44.       
  45.     collectionView.backgroundColor=[UIColor whiteColor];  
  46.     collectionView.delegate=self;  
  47.     collectionView.dataSource=self;  
  48.     [self.view addSubview:collectionView];  
  49. }  
  50.   
  51. #pragma mark  返回多少行  
  52. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  53. {  
  54.      
  55.     return 13;  
  56. }  
  57. #pragma markk 返回的单元格  
  58. -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  59. {  
  60.     ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];  
  61.       
  62.     return cell;  
  63. }  
  64.   
  65. //  返回头视图  
  66. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath  
  67. {  
  68.     //如果是头视图  
  69.     if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {  
  70.          UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];  
  71.         //添加头视图的内容  
  72.         [self addContent];  
  73.         //头视图添加view  
  74.         [header addSubview:self.headerImage];  
  75.         return header;  
  76.     }  
  77.     //如果底部视图  
  78. //    if([kind isEqualToString:UICollectionElementKindSectionFooter]){  
  79. //          
  80. //    }  
  81.     return nil;  
  82. }  
  83. /* 
  84.  *  补充头部内容 
  85.  */  
  86. -(void)addContent  
  87. {  
  88.     UIImageView *headerImage=[[UIImageView alloc]init];  
  89.     headerImage.contentMode=UIViewContentModeScaleAspectFill;  
  90.     headerImage.clipsToBounds=YES;  
  91.     headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);  
  92.     headerImage.image=[UIImage imageNamed:@"mei"];  
  93.     self.headerImage=headerImage;  
  94. }  
  95.   
  96.   
  97. @end  

代码的运行结果如下图



查看我的更多开源项目点击(开源项目)

原文地址:https://www.cnblogs.com/yecong/p/6075603.html