There are two ways to use uicollectionview like table view
One is to inherit Uicollectionviewcontroller, and this controller will bring a uicollectionview.
The second is to create a uiconllectionview view that is placed in a normal uiviewcontroller.
We use a different
first declare a declaration first Reuse Markers and implementation of trust
#define _cell @"Acell"
@interfaceyxpviewcontroller () <Uicollectionviewdatasource, uicollectionviewdelegate , Uicollectionviewdelegateflowlayout >
Then initialize the Uicollectionview
-(void) Initcollectionview
{
//Instantiate a layer first
uicollectionviewflowlayout *layout=[[uicollectionviewflowlayoutalloc] init];
//Create one-screen view size
uicollectionview *collectionview=[[uicollectionviewalloc] initwithframe: self. View. bounds collectionviewlayout: layout];
[CollectionViewregisterclass: [Uicollectionviewcellclass] Forcellwithreuseidentifier:_cell];
CollectionView. backgroundcolor=[uicolorwhitecolor];
CollectionView. delegate=self;
CollectionView. DataSource=self;
[Self. View addsubview: CollectionView];
}
Implementing Proxy methods
#pragma Mark--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;
}
The content of each Uicollectionview display
-(uicollectionviewcell *) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: (nsindexpath *) Indexpath
{
Uicollectionviewcell * cell = [CollectionViewdequeuereusablecellwithreuseidentifier :_cellforindexpath: Indexpath];
cell.BackgroundColor = [UicolorColorwithred:((Arc4random()%255)/255.0)Green:((Arc4random()%255)/255.0)Blue:((Arc4random()%255)/255.0)Alpha:1.0f];
return cell;
}
#pragma Mark--uicollectionviewdelegate
method to invoke when Uicollectionview is selected
-(void) CollectionView: (uicollectionview *) CollectionView Didselectitematindexpath: ( Nsindexpath *) Indexpath
{
Uicollectionviewcell * cell = (uicollectionviewcell *) [CollectionViewCellforitematindexpath : Indexpath];
cell.BackgroundColor = [UicolorColorwithred:((Arc4random()%255)/255.0)Green:((Arc4random()%255)/255.0)Blue:((Arc4random()%255)/255.0)Alpha:1.0f];
}
Returns whether this uicollectionviewcell is enough to be chosen
-(BOOL) CollectionView: (uicollectionview *) CollectionView Shouldselectitematindexpath: ( Nsindexpath *) Indexpath
{
return YES;
}
#pragma Mark--uicollectionviewdelegateflowlayout
Define the size of each Uicollectionview
-(cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (nsindexpath *) Indexpath
{
return Cgsizemake (a);
}
Define the margins for each Uicollectionview
-(uiedgeinsets) CollectionView: (uicollectionview *) CollectionView layout: ( Uicollectionviewlayout *) collectionviewlayout insetforsectionatindex: (nsinteger) Section
{
return Uiedgeinsetsmake (Ten, 10 ,ten);
}
so a simple uicollection view is complete.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
iOS Foundation _ Uicollectionview Easy to use