CollectionView How to add a header view

Source: Internet
Author: User

Often have friends in the group to ask CollectionView how to add head view, more than one asked, a lot of ask, so small make up simply write a collectionview how to add head view of the blog, for everyone to reference learning. (Sample code download address)

We want to use the header view inside the CollectionView to register the header view Uicollectionreusableview or to inherit the Uicollectionreusableview subclass. Kind type is Uicollectionelementkindsectionheader and requires an identifier, we define a static string on the line, as follows:

[CollectionView Registerclass:[uicollectionreusableviewclass] Forsupplementaryviewofkind: Uicollectionelementkindsectionheader Withreuseidentifier:headerviewidentifier];

A few important points that can not be overlooked is uicollectionviewflowlayout layout properties need to set the size of the headerreferencesize head, otherwise the header view is not the size of the display; be sure to

-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath; method creates a header view and gives a frame and then adds it to the Uicollectionreusableview. Detailed code is as follows

<pre name= "code" class= "OBJC" >////HOMEVIEWCONTROLLER.M//Collection Add head////Created by 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. Add CollectionView [self addcollectionview];
    }-(void) Addcollectionview {uicollectionviewflowlayout *layout=[[uicollectionviewflowlayout alloc]init]; layout.minimumlinespacing=20;  Set the spacing between each row 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 header 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 Header 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} #pragma markk returned cell-(uicollectionviewcell*) CollectionView: (Uicollectionview *) Collection View Cellforitematindexpath: (Nsindexpath *) Indexpath {Constomcell *cell=[collectionview dequeuereusablecellwithreuseidentifier:@ "Cell" forInDexpath:indexpath];
return cell; //Return Header View-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (NSString *) kind Atindexpath: (Nsindexpath *) Indexpath {//If it is Head view if ([Kind isequ Altostring:uicollectionelementkindsectionheader]) {Uicollectionreusableview *header=[collectionview DequeueReusa
        Blesupplementaryviewofkind:kind Withreuseidentifier:headerviewidentifier Forindexpath:indexpath];
        Add the contents of the header view [self addcontent];
        Header view [header AddSubview:self.headerImage];
    return header;
//If the bottom view//if ([kind Isequaltostring:uicollectionelementkindsectionfooter]) {////} return nil;
    * * * Supplemental 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

The code runs as shown below



See more of my open source projects Click (Open Source project)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.