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 commonly used methods. (only common, others can see the relevant API)
#pragmaMark--Uicollectionviewdatasource//defines the number of Uicollectionviewcell to show-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) Section {return -; } //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 =@"Gradientcell"; 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]; 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; }
The advent of IOS CollectionView is a great benefit, no longer using TableView to define complex multi-column tables, similar to table, except that the cell must be added by itself, with no default mode
Since the CollectionView does not have a default cell layout, it is generally easy and fast to customize
One, custom cell
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.
Second, the definition of uicollectionview;
1. Drag a collection view to the specified Viewcontroller view
2. Connect DataSource and delegate, and create a map named CollectionView
3. Select the CollectionView ruler to change the width and height of the cell size to the same 95*116 as the custom cell, figure 8
4. Check the properties of CollectionView, you can modify its properties, such as vertical sliding, or horizontal sliding, select vertical or Horizontal
5, select Collectionviewcell, modify class, Inherit from Collectioncell
5, in the Viewdidload method declaration cell class, added in the Viewdidload method, this sentence does not declare, will not load, program crashes
Where Collectioncell is the identity of the cell (the previous steps have already been defined.) )
class] Forcellwithreuseidentifier:@ "collectioncell"];
6. Declare the agent in ViewController.h
@interface viewcontroller:uiviewcontroller<uicollectionviewdatasource,uicollectionviewdelegate>
7. Implementing proxy Methods in. m files
//Item count for each section-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) Section {return A; } -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {Collectioncell*cell = (Collectioncell *) [CollectionView dequeuereusablecellwithreuseidentifier:@"Collectioncell"Forindexpath:indexpath]; //Picture nameNSString *imagetoload = [NSString stringWithFormat:@"%d.png", Indexpath.row]; //Loading PicturesCell.imageView.image =[UIImage Imagenamed:imagetoload]; //Set Label textCell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(Long) Indexpath.row, (Long) indexpath.section]; returncell; }
8. Effect 10
Click on an item after the jump event similar to UITableView, implement proxy method
-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) Indexpath
iOS Development-Uicollectionview + examples