To create a Collectioncell template:
1, new class Collectioncell inherit from Uicollectionviewcell
2, New Xib, named Collectioncell.xib
A. Select Collectioncell.xib to delete the default view, drag a collection view Cell from the control (Figure 3) to the canvas, and set the size to 95*116;
B. Select the cell that you just added, change the class name to collectioncell,4
C. Add a ImageView and a label to the Collectioncell of Collectioncell.xib (Figure 5)
D. Creating a map, Figure 6, Figure 7
E. Select collectioncell.m, overriding the Init method
- (ID) initWithFrame: (CGRect) frame { self=[Super Initwithframe:frame]; if(self) {//load Collectioncell.xib file when initializingNsarray *arrayofviews = [[NSBundle mainbundle] loadnibnamed:@"Collectioncell"owner:self Options:nil]; //if the path does not exist, return nil if(Arrayofviews.count <1) { returnNil; } //If the Xib view does not belong to the Uicollectionviewcell class, return nil if(! [[Arrayofviews Objectatindex:0] Iskindofclass:[uicollectionviewcellclass]]) { returnNil; } //Load NIBSelf = [Arrayofviews objectatindex:0]; } returnSelf ; }
F. Select Collectioncell.xib to modify its identifier to Collectioncell.
======================================================
Indexviewcontroller.h
#import <UIKit/UIKit.h>#import"CollectionCell.h"@ Interface Indexviewcontroller:uiviewcontroller <uicollectionviewdatasource,uicollectionviewdelegate, uicollectionviewdelegateflowlayout>@end
Indexviewcontroller.m
#import "Indexviewcontroller.h"@interfaceIndexviewcontroller ()@end@implementationIndexviewcontrolleruicollectionview*CV;- (void) viewdidload{[Super Viewdidload]; [Self Loadcollectionview];}-(void) loadcollectionview{uicollectionviewflowlayout*flowlayout =[[Uicollectionviewflowlayout alloc]init]; [FlowLayout Setitemsize:cgsizemake ( the, -)];//set the cell size[FlowLayout Setscrolldirection:uicollectionviewscrolldirectionhorizontal];//set its layout directionCV=[[uicollectionview Alloc]initwithframe:cgrectmake ( +, -, -, -) Collectionviewlayout:flowlayout]; [CV Registerclass:[collectioncellclass] Forcellwithreuseidentifier:@"Collectioncell"]; [CV setbackgroundcolor:[uicolor colorwithred:255.0f/255.0fGreen255.0f/255.0fBlue255.0f/255.0fAlpha0.75]]; CV.Delegate=Self ; Cv.datasource=Self ; [Self.view ADDSUBVIEW:CV];}#pragmaMark--Uicollectionviewdatasource//defines the number of Uicollectionviewcell to show-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return 6;}//define the number of sections to show-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 1;}//what each Uicollectionview shows-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{StaticNSString * Cellidentifier =@"Collectioncell"; Uicollectionviewcell* Cell =[CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath]; Cell.backgroundcolor=[Uicolor Graycolor]; returncell;}#pragmaMark--uicollectionviewdelegateflowlayout//define the size of each Uicollectionview-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{returnCgsizemake ( the, -);}//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]; Cell.backgroundcolor=[Uicolor Whitecolor];}//returns whether this Uicollectionview can be selected-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) indexpath{returnYES;}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
[IOS uicollectionview templates]