CollectionView How to add a head view

Source: Internet
Author: User

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

  1. <pre name= "code" class="OBJC" >//
  2. Homeviewcontroller.m
  3. Collection adding a head
  4. //
  5. Created by the 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. Adding CollectionView
  18. [self addcollectionview];
  19. }
  20. -(void) Addcollectionview
  21. {
  22. uicollectionviewflowlayout *layout=[[uicollectionviewflowlayout Alloc]init];
  23. Layout. minimumlinespacing=20; //Set spacing for each line
  24. Layout. Itemsize=cgsizemake (100, 100); //Set the size of each cell
  25. Layout. Sectioninset=uiedgeinsetsmake (0, 0, 50, 0);
  26. Layout. Headerreferencesize=cgsizemake (self. View. Frame. Size. Width, 250); //Set the size of the CollectionView head view
  27. Uicollectionview *collectionview=[[uicollectionview alloc]initwithframe:self. View.  Bounds Collectionviewlayout:layout];
  28. CollectionView. frame=self. View. Bounds;
  29. //Register cell cells
  30. [CollectionView registernib:[uinib nibwithnibname:@ "Constomcell" bundle: nil]   Forcellwithreuseidentifier:@ "Cell"];
  31. //Register head view
  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. How many rows #pragma mark returns
  39. -(Nsinteger) CollectionView: (uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
  40. {
  41. return 13;
  42. }
  43. #pragma cells returned by MARKK
  44. -(uicollectionviewcell*) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) Indexpath
  45. {
  46. Constomcell *cell=[collectionview dequeuereusablecellwithreuseidentifier:@ "Cell" Forindexpath:  Indexpath];
  47. return cell;
  48. }
  49. Return to head view
  50. -(Uicollectionreusableview *) CollectionView: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind atindexpath: (nsindexpath *) Indexpath
  51. {
  52. //If the head view
  53. if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {
  54. uicollectionreusableview *header=[collectionview dequeuereusablesupplementaryviewofkind:kind   Withreuseidentifier:headerviewidentifier Forindexpath:indexpath];
  55. //Add the contents of the head view
  56. [self addcontent];
  57. //Head view Add view
  58. [Header addsubview:selfheaderimage];
  59. return header;
  60. }
  61. //If the bottom view
  62. if ([Kind Isequaltostring:uicollectionelementkindsectionfooter]) {
  63. //
  64. //    }
  65. return Nil;
  66. }
  67. /*
  68. * Add header Content
  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

Final effect

CollectionView How to add a head view

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.