1 , overview
The Uicollectionview control is primarily used to make nine Gongge, similar to the GridView control in Android. As with UITableView, you must first make the controller conform to the data source protocol, and then set the controller to the Uicollectionview data source. Similarly, the controller complies with the Uicollectionview agent can also implement proxy methods and so on.
2 , common methods of data sources
Set Uicollectionviewcontroller How many groups are there altogether:
-(Nsinteger)numberofsectionsincollectionview:(Uicollectionview
*) CollectionView;
Set how many cells are in each group:
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView
numberofitemsinsection:(nsinteger) section;
Set the contents of each cell display:
-(Uicollectionviewcell *) CollectionView: (Uicollectionview
*) CollectionView cellforitematindexpath:(Nsindexpath *) Indexpath;
3 , commonly used proxy methods
Set each cell Click event:
-(void) CollectionView: (Uicollectionview *) CollectionView
Didselectitematindexpath:(Nsindexpath *) Indexpath
{
Mjproduct *p = Self.products[indexpath.item];
NSLog (@ "Clicked---%@", p.title);
}
4 , Uicollectionviewcontroller methods that must be called
(1) Register cell ( tell collectionview What cell to create in the future )
[Self.collectionview Registerclass:[uicollectionviewcell class]
forcellwithreuseidentifier:@ "Product"];
In Call-(Uicollectionviewcell *) CollectionView: (Uicollectionview
*) CollectionView Cellforitematindexpath: (Nsindexpath *) Indexpath; You must register the cell before you set the contents of each cell (Uicollectionviewcell) The above method is typically called in viewdidload to register the cell.
For example:
-(void) viewdidload
{
[Super Viewdidload];
1. Register the cell (Tell CollectionView what cell to create in the future)
uinib *nib = [uinib nibwithnibname:@ "Mjproductcell" bundle:nil];
[Self.collectionview registernib:nib Forcellwithreuseidentifier:
mjproductcellid];//Custom cell via XID
/*
[Self.collectionview Registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:mjproductcellid];// Use the default Uicollectionviewcell
*/
2. Set the background color of the CollectionView
Self.collectionView.backgroundColor = [Uicolor Whitecolor];
}
(2) removing from the cache pool Cell
-(Uicollectionviewcell *) CollectionView: (Uicollectionview
*) CollectionView Cellforitematindexpath: (Nsindexpath *) Indexpath
{
Uicollectionviewcell *cell =
[CollectionView dequeuereusablecellwithreuseidentifier:@ "Product" Forindexpath:indexpath];
return cell;
}
When taking Uicollectionviewcell reuse from a cache pool, it is not necessary to determine whether the cache pool is empty, unlike UITableView, which Uicollectionview automatically creates a new cell when the cell is not found in the cache pool.
(3) rewrite Init Method , Creating layout Parameters
-(ID) init
{
1. Flow Layout
Uicollectionviewflowlayout *layout =
[[Uicollectionviewflowlayout alloc] init];
2. Size of each cell
Layout.itemsize = Cgsizemake (80, 80);
3. Set the horizontal spacing between cells
layout.minimuminteritemspacing = 0;
4. Set the vertical spacing between cells
layout.minimumlinespacing = 10;
5. Set up a whole cell of all cells with the screen (Viewcontroller) around the distance
Layout.sectioninset =
Uiedgeinsetsmake (layout.minimumlinespacing, 0, 0, 0);
return [Super Initwithcollectionviewlayout:layout];
}
If you do not create a layout parameter, the program will error. Typically created in the Init method that overrides the controller.
5 , Uicollectionviewflowlayout
Uicollectionviewflowlayout is called a "flow layout" to constrain the display of cells.
Common Properties:
Cell Size:
@property (nonatomic) cgsize itemsize;
Horizontal spacing between cells:
@property (nonatomic) cgfloat minimuminteritemspacing;
Vertical spacing between cells:
@property (nonatomic) cgfloat minimumlinespacing;
Four-week padding:
@property (nonatomic) uiedgeinsets sectioninset;
iOS Development Uicollectionviewcontroller