Everyone said that Colletionview and Uitabbleview are brothers, and Colletionview is out after iOS 6, Colletionview and Uitabbleview they are really brothers, but used when you encounter a lot of pits.
Like what:
Uicollectionview *colletionview = [[Uicollectionview alloc]init]; initialize a colletionview, if you do, you fall into the pit. This is the official document for others.
So!!!! You're going to blow it off like that. That's all you can do.
Uicollectionviewflowlayout*grid =[[UicollectionviewflowlayoutALLOC] init]; Grid.itemsize =Cgsizemake( the, the);//Set the size of the colletionview Grid.sectioninset =Uiedgeinsetsmake(10.0,Ten,Ten,Ten);Uicollectionview*colletionview = [[UicollectionviewAllocinitWithFrame: Self. view.framecollectionviewlayout:Grid];colletionview.delegate = Self; Colletionview.datasource = Self; [Colletionviewregisterclass:[Photocell class] forcellwithreuseidentifier: @ "Simplecell"];//This must add not to add your cell init will not be called Oh!!
OK is successfully instantiating a colletionview it's like its brother. You need to set up a proxy and set up a data source. It has been set up at the time of instantiation.
Now go and implement his proxy and customize a Colletionviewcell
Custom cell is going to customize a cell class to inherit Uicollectionviewcell
. h
#import <UIKit/UIKit.h>@interface Cell : UICollectionViewCell@property (nonatomic,strong)UIImageView *image;@end
. m
@implementation Cell -(ID) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self) {//Change to our custom selected background view Self. Image= [[UiimageviewAlloc]init]; [ Self. ImageSetframe:cgrectmake ( Self. Contentview. Frame. Size. Width/2+ -+5, -5, -, -)]; [ Self. Imagesetbackgroundcolor:[UicolorRedcolor]]; Self. Image. Layer. Cornerradius= Self. Image. Frame. Size. Width/2;//Set the border of a layer to rounded corners Self. Image. Layer. Maskstobounds=YES;//Hide Border[ SelfAddsubview: Self. Image]; }return Self;}@end
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 1;} -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (Nsinteger) section{return Ten;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath*) indexpath{Static NSString*cellidentifier = @"Simplecell"; Cell *cell = (cell *) [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath];if(Cell = =Nil) {cell = [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath]; }returnCell;} - (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath*) indexpath{//--Single Event}
IOS Custom Colletionview (pure code)