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 中。

詳細代碼如下

  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  

 

最終效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM