After iOS6.0, Apple launched a new view that inherits from Uiscrolleriew, Uicollectionview, also known as a collection view. Together with UITableView as the two views commonly used in development, it often appears as the main interface of the project.
Code Demo:
#import "YourCollectionViewCell.h"@implementationYourcollectionviewcell-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {//get overall item width and heightCGFloat totalWidth =Frame.size.width; CGFloat totalheight=Frame.size.height; Self.numberlabel= [[UILabel alloc]initwithframe:cgrectmake (0,Ten, TotalWidth, Totalheight- -)]; Self.numberLabel.textAlignment=Nstextalignmentcenter; Self.numberLabel.font= [Uifont boldsystemfontofsize: $]; [Self.contentview AddSubview:self.numberLabel]; }returnSelf ;}@end#import "MyCollectionReusableView.h"@implementationMycollectionreusableview-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Self.headerlabel=[[UILabel alloc]initwithframe:self.bounds]; Self.headerLabel.textAlignment=Nstextalignmentcenter; Self.headerLabel.font= [Uifont boldsystemfontofsize: $]; [Self AddSubview:self.headerLabel]; }returnSelf ;}@end#import "ViewController.h"#import "YourCollectionViewCell.h"#import "MyCollectionReusableView.h"#defineKreuse @ "Reuse"#defineKwidth Self.view.frame.size.width@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Create a layout object that uses the system layout class UicollectionviewflowlayoutUicollectionviewflowlayout*layout =[[Uicollectionviewflowlayout alloc]init]; /*//Set minimum line spacing layout.minimumlinespacing = 20; Sets the spacing between item and item layout.minimuminteritemspacing = 10; Layout.sectioninset = Uiedgeinsetsmake (10, 10, 10, 10); Set the size of each item cgfloat totalWidth = Self.view.frame.size.width; Layout.itemsize = Cgsizemake ((totalWidth-40)/3, 80); */ //set the slide directionLayout.scrolldirection =uicollectionviewscrolldirectionvertical; //the creation of the collection view must specify the layout, and if there is no layout, nothing can be displayed .Uicollectionview *collectionview =[[Uicollectionview alloc]initwithframe:[[uiscreen mainscreen]bounds] collectionviewlayout:layout]; Collectionview.datasource=Self ; CollectionView.Delegate=Self ; //Collection View If you want to display content, you must register the cell[CollectionView Registerclass:[yourcollectionviewcellclass] Forcellwithreuseidentifier:kreuse]; //Collection View If you want the partition header view to appear, you must register the augmented view[CollectionView Registerclass:[mycollectionreusableviewclass] Forsupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:@"Header"]; //to the Head view layoutLayout.headerreferencesize = Cgsizemake (Kwidth, +); [Self.view Addsubview:collectionview]; }-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{returnCgsizemake (Kwidth- +)/3, the);}-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Insetforsectionatindex: (nsinteger) section{returnUiedgeinsetsmake (0,0,0,0);}-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) section{return -;}//-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section{//return;//}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-------Collection View data source protocol--------(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return the;}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Yourcollectionviewcell*cell =[CollectionView dequeuereusablecellwithreuseidentifier:kreuse Forindexpath:indexpath]; Cell.contentView.backgroundColor= [Uicolor colorwithred:arc4random ()% the/255.0Green:arc4random ()% the/255.0Blue:arc4random ()% the/255.0Alpha1.0]; Cell.numberLabel.text= [NSString stringWithFormat:@"%ld", Indexpath.row]; returncell;}//returns the augmented view, which is the head or tail view of the collection view-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{Mycollectionreusableview*view = [CollectionView dequeuereusablesupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:@"Header"Forindexpath:indexpath]; View.headerLabel.text= [NSString stringWithFormat:@"Current partition is:%ld", indexpath.section]; View.backgroundcolor=[Uicolor Yellowcolor]; returnview;}//set Number of partitions-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return Ten;}#pragmaMark-------The Collection View Proxy method------(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{NSLog (@"%ld,%ld", Indexpath.section,indexpath.row);}@end
iOS Basics CollectionView (collection View)