Directly on the code, please read the comments.
1. Inheritance of three agents uicollectionviewdatasource,uicollectionviewdelegate,uicollectionviewdelegateflowlayout
2. Look directly at the code
#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload]; //determine whether to scroll horizontally or verticallyUicollectionviewflowlayout *flowlayout=[[Uicollectionviewflowlayout alloc] init]; [FlowLayout setscrolldirection:uicollectionviewscrolldirectionvertical]; //you can not use the FlowLayout proxy, directly as below to give a value//[FlowLayout setminimumlinespacing:0];//Bottom Blank Empty//[FlowLayout setminimuminteritemspacing:0];//left and right empty empty//flowlayout.sectioninset = uiedgeinsetsmake (0, 0, 8, 0);//define margin for eachSelf.collectionview=[[uicollectionview Alloc] Initwithframe:cgrectmake (0, -, the, $) Collectionviewlayout:flowlayout]; Self.collectionView.dataSource=Self ; Self.collectionview.Delegate=Self ; [Self.collectionview Setbackgroundcolor:[uicolor Clearcolor]; //To register a cell, you must have[Self.collectionview Registerclass:[uicollectionviewcellclass] Forcellwithreuseidentifier:@"Uicollectionviewcell"]; [Self.view AddSubview:self.collectionView];}#pragmaMark--Uicollectionviewdatasource//defines the number of Uicollectionviewcell to show-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return Ten;}//define the number of sections to show-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 2;}//what each Uicollectionview shows-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{StaticNSString * Cellidentifier =@"Uicollectionviewcell"; Uicollectionviewcell* Cell =[CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath]; Cell.backgroundcolor= [Uicolor colorwithred: ((Ten* Indexpath.row)/255.0) Green: (( -* Indexpath.row)/255.0) Blue: (( -* Indexpath.row)/255.0) Alpha:1.0f]; UILabel*label = [[UILabel alloc] Initwithframe:cgrectmake (0,0, -, -)]; Label.textcolor=[Uicolor Redcolor]; Label.text= [NSString stringWithFormat:@"%d", Indexpath.row]; for(IDSubViewinchcell.contentView.subviews) {[SubView Removefromsuperview]; } [Cell.contentview Addsubview:label]; returncell;}#pragmaMark--uicollectionviewdelegateflowlayout//define the size of each item-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{returnCgsizemake ( -, -);}//define margin for each Uicollectionview-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout Insetforsectionatindex: (nsinteger) section{returnUiedgeinsetsmake (5,5,5,5);}#pragmaMark--uicollectionviewdelegate//method to invoke when Uicollectionview is selected-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{Uicollectionviewcell* Cell = (Uicollectionviewcell *) [CollectionView Cellforitematindexpath:indexpath]; //temporarily change a color, optimistic, just temporary change. If you want to change permanently, you can first change the data source and then control it in Cellforitematindexpath. (It's almost like UITableView!) O (∩_∩) o~)Cell.backgroundcolor =[Uicolor Greencolor]; NSLog (@"item======%d", Indexpath.item); NSLog (@"row=======%d", Indexpath.row); NSLog (@"section===%d", indexpath.section);}//returns whether this Uicollectionview can be selected-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) indexpath{returnYES;}@end
Reference Blog: http://blog.csdn.net/u011439689/article/details/39551163?utm_source=tuicool&utm_medium=referral
IOS UI Basics -19.0 Uicollectionview