First, meet Uicollectionview.
@interface Uicollectionview:uiscrollview
The Uicollectionview and Uicollectionviewcontroller classes are IOS6 's newly introduced APIs for presenting collection views, with a more flexible layout that enables multi-column layouts, similar to UITableView and Uitableviewcontroller class.
Using Uicollectionview must implement Uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout these three agreements.
Here are some common methods, specific use can refer to the demo: point I download Apple official demo: click I download
- (void) viewdidload {[Super viewdidload]; Self.title=@"Uicollectionview Learning"; //generate a cell from nib, and then register the nib's view to inherit Uicollectionviewcell[Self.collectionview registernib:[uinib Nibwithnibname:@"Sqcollectioncell"Bundle:nil] forcellwithreuseidentifier:kcellidentifier]; //registering Headerview nib needs to inherit the view Uicollectionreusableview[Self.collectionview registernib:[uinib Nibwithnibname:@"Sqsupplementaryview"Bundle:nil] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier: Kheaderidentifier]; //registering Footerview nib needs to inherit the view Uicollectionreusableview[Self.collectionview registernib:[uinib Nibwithnibname:@"Sqsupplementaryview"Bundle:nil] Forsupplementaryviewofkind:uicollectionelementkindsectionfooter withreuseidentifier: Kfooteridentifier]; //Self.collectionView.allowsMultipleSelection = YES;//The default is no, whether you can select multiple } - (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated. } #pragmaMark-collectionview DataSource// Section-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView {return 2; } //Item Count-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) Section {return 6; } //The cell is returned must was retrieved from a call To-dequeuereusablecellwithreuseidentifier:forindexpath: -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {//Reusing cellsUicollectionviewcell *cell =[CollectionView dequeuereusablecellwithreuseidentifier:kcellidentifier Forindexpath:indexpath]; //Assign ValueUiimageview *imageview = (Uiimageview *) [cell Viewwithtag:1]; UILabel*label = (UILabel *) [cell Viewwithtag:2]; NSString*imagename = [NSString stringWithFormat:@"%ld. JPG",(Long) Indexpath.row]; Imageview.image=[UIImage Imagenamed:imagename]; Label.text=ImageName; Cell.backgroundcolor=[Uicolor Redcolor]; returncell; } //The view that's returned must be retrieved from a call To-dequeuereusablesupplementaryviewofkind:withreuseidentifie R:forindexpath:-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{NSString*Reuseidentifier; if([Kind Isequaltostring:uicollectionelementkindsectionfooter]) {reuseidentifier=Kfooteridentifier; }Else{reuseidentifier=Kheaderidentifier; } Uicollectionreusableview*view =[CollectionView dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:reuseidentifier ForIndexPath: Indexpath]; UILabel*label = (UILabel *) [view Viewwithtag:1]; if([Kind Isequaltostring:uicollectionelementkindsectionheader]) {Label.text= [NSString stringWithFormat:@"this is header:%d .", indexpath.section]; } Else if([Kind Isequaltostring:uicollectionelementkindsectionfooter]) {View.backgroundcolor=[Uicolor Lightgraycolor]; Label.text= [NSString stringWithFormat:@"this is footer:%d .", indexpath.section]; } returnview; } //define the size of each Uicollectionviewcell-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath {returnCgsizemake ( -, the); } //define margin for each section-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout Insetforsectionatindex: (nsinteger) Section {returnUiedgeinsetsmake ( the, the,5, the);//top, left, bottom, right, respectively} //returns the size of the head Headerview-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout referencesizeforheaderinsection: (nsinteger) section{cgsize size={ the, $}; returnsize; } //returns the size of the head Footerview-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout referencesizeforfooterinsection: (nsinteger) Section {cgsize size={ the, $}; returnsize; } //line spacing between different rows in each section-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) Section {return Ten; } //the spacing between each item//-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section//{ //return 100; //} //a cell was selected- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) Indexpath {Uicollectionviewcell*cell =[CollectionView Cellforitematindexpath:indexpath]; [Cell Setbackgroundcolor:[uicolor Greencolor]]; } //deselect a cell- (void) CollectionView: (Uicollectionview *) CollectionView Diddeselectitematindexpath: (Nsindexpath *) Indexpath {Uicollectionviewcell*cell =[CollectionView Cellforitematindexpath:indexpath]; [Cell Setbackgroundcolor:[uicolor Redcolor]]; }
As follows:
IOS Uicollectionview Simple to use