We want to use the head view inside the CollectionView to first register the header view Uicollectionreusableview or inherit the Uicollectionreusableview subclass, The kind type is Uicollectionelementkindsectionheader and requires an identifier, and we define a static string that is statically the line, as follows:
[CollectionView registerclass: [Uicollectionreusableviewclass ] forsupplementaryviewofkind :uicollectionelementkindsectionheader withreuseidentifier:headerviewidentifier ];
A few important points that can not be overlooked is that uicollectionviewflowlayout layout properties need to set the size of the headerreferencesize head, otherwise the head view is not the size of the display;
-(uicollectionreusableview *) CollectionView: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind Atindexpath: (nsindexpath *) Indexpath; Method creates a header view view and gives the frame, and then adds it to the Uicollectionreusableview.
The detailed code is as follows
- <pre name= "code" class="OBJC" >//
- Homeviewcontroller.m
- Collection adding a head
- //
- Created by the user on 15/10/10.
- Copyright (c) 2015 user. All rights reserved.
- //
- #import "HomeViewController.h"
- #import "ConstomCell.h"
- Static NSString *headerviewidentifier = @ "Hederview";
- @interface Homeviewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate>
- @property (nonatomic,Strong) Uiimageview *headerimage;
- @end
- @implementation Homeviewcontroller
- -(void) Viewdidload {
- [Super Viewdidload];
- //1. Adding CollectionView
- [self addcollectionview];
- }
- -(void) Addcollectionview
- {
- uicollectionviewflowlayout *layout=[[uicollectionviewflowlayout Alloc]init];
- Layout. minimumlinespacing=20; //Set spacing for each line
- Layout. Itemsize=cgsizemake (100, 100); //Set the size of each cell
- Layout. Sectioninset=uiedgeinsetsmake (0, 0, 50, 0);
- Layout. Headerreferencesize=cgsizemake (self. View. Frame. Size. Width, 250); //Set the size of the CollectionView head view
- Uicollectionview *collectionview=[[uicollectionview alloc]initwithframe:self. View. Bounds Collectionviewlayout:layout];
- CollectionView. frame=self. View. Bounds;
- //Register cell cells
- [CollectionView registernib:[uinib nibwithnibname:@ "Constomcell" bundle: nil] Forcellwithreuseidentifier:@ "Cell"];
- //Register head view
- [CollectionView Registerclass:[uicollectionreusableview class] Forsupplementaryviewofkind: Uicollectionelementkindsectionheader Withreuseidentifier:headerviewidentifier];
- CollectionView. Backgroundcolor=[uicolor Whitecolor];
- CollectionView. delegate= self;
- CollectionView. datasource= self;
- [self. View Addsubview:collectionview];
- }
- How many rows #pragma mark returns
- -(Nsinteger) CollectionView: (uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
- {
- return 13;
- }
- #pragma cells returned by MARKK
- -(uicollectionviewcell*) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) Indexpath
- {
- Constomcell *cell=[collectionview dequeuereusablecellwithreuseidentifier:@ "Cell" Forindexpath: Indexpath];
- return cell;
- }
- Return to head view
- -(Uicollectionreusableview *) CollectionView: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind atindexpath: (nsindexpath *) Indexpath
- {
- //If the head view
- if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {
- uicollectionreusableview *header=[collectionview dequeuereusablesupplementaryviewofkind:kind Withreuseidentifier:headerviewidentifier Forindexpath:indexpath];
- //Add the contents of the head view
- [self addcontent];
- //Head view Add view
- [Header addsubview:selfheaderimage];
- return header;
- }
- //If the bottom view
- if ([Kind Isequaltostring:uicollectionelementkindsectionfooter]) {
- //
- // }
- return Nil;
- }
- /*
- * Add header Content
- */
- -(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
Final effect
CollectionView How to add a head view