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

最终效果

时间: 2024-12-22 22:53:48

collectionView怎么添加头视图的相关文章

UI tableView 的头视图 &amp; &quot;小广告&quot;

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.dataSource =

&quot;UICollectionView实现带头视图和组的头视图同时存在&quot;实现

实现效果如下: 以前做这效果的界面,总是实现的是section的头视图,因为我一直觉得collectionView是不像UITableView那样有tableHeaderView的,所以每次实现只能是判断indexpath.section为0的时候,组合一下视图做第一个section的头视图. 今天看别人写的Swift项目,看到人家代码的实现这效果的简便,我实在是不敢相信这么容易,于是自己赶紧用OC写了个简单的demo,发现还真是能实现呢......好开心.... 实现的源码如下,注释的很清楚啦

//快速添加一个视图控制器

//快速添加一个视图控制器 #import <UIKit/UIKit.h> @interface UITabBarController (ZJTabBarExtension) -(UIViewController*)andViewController:(Class)cls title:(NSString*)title image:(NSString*)image; @end @implementation UITabBarController (ZJTabBarExtension) -(UIV

表视图展开收起 头视图滑动到顶部不动 直到该组单元格全部推上去

类似qq的分组  头视图展开后推到屏幕顶端不动直到该组的单元格全部推上去 实现方法 1.在下面创建表视图的方法里style 设置成UITableViewStylePlain [[ UITableView alloc] initWithFrame:CGRectMake(0, 100, kScreenWide, kScreenHeight - 164) style:UITableViewStylePlain]; 2.表视图的代理方法 要添加设置组的代理方法 - (NSInteger)numberOf

精通IOS-在表单元中添加子视图

#import <UIKit/UIKit.h> @interface NameAndColorCellTableViewCell : UITableViewCell @property(copy,nonatomic) NSString *name; @property(copy,nonatomic) NSString *color; @end NameAndColorCellTableViewCell.h // // NameAndColorCellTableViewCell.m // Tab

ListView添加头布局和脚布局

 ListView添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考: 如果我们当前的页面有多个接口.多种布局的话,我们一般的选择无非就是1.多布局:2.各种复杂滑动布局外面套一层ScrollView(好low):3.头布局脚布局.有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了:学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了: 效果图:                   正文部分布局: fragment_cla

新浪微博开发-添加子视图控制器&amp;设置颜色

一.添加子视图控制器 二.设置颜色 设置颜色:两种方法 一种较为繁琐,详见视频 第二种: //设置颜色 self.tabBar.tintColor = UIColor.orangeColor()

关于cell中添加子视图 复用重叠问题的解决方法

问题本质: 因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创建一个子视图添(button,label之类的)加进去,会给占用很大的内存,所以采用了复 用的方法,但是问题就来了,当cell超出界面,从队列中拿过来复用的时候,其中子视图的内容并没有消除,这样你会原来的基础上再创建一个子视图添加上去 遮住了原来的视图,一般视图都是透明的这样的话就变成了一层层的叠加.

手把手教你给RecycleView添加头布局和尾布局

RecycleView想必大家都不陌生,它已他的高拓展性取代了传统布局显示,同时配合协调布局,可以实现很多意想不到的酷炫交互,今天就和大家介绍一下,如何给RecycleView添加头布局和尾布局,同时你也可以通过自己的拓展实现更多复杂的布局. 首先我们先看一下效果: 实现头部尾部布局其实方法还是很多,这里我推荐使用Adapter设置itemType来做,那么问题来了,为什么这么做呢?因为拓展性更强,并且更加解耦.将逻辑都写在adapter中,比起写在activity中管理起来也更加方便. 首先,