1. In storyboard, drag out 1 Uicollectionviewcontroller
2. New File--cocoa Touch Class, inherited from Uicollectionviewcontroller, assuming the name is Collectiondemo
3. In storyboard, change the Uicollectionviewcontroller class that you just dragged out to Collectiondemo
4. Implementation in COLLECTIONDEMO.M, data source method
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
{
return self.photos.count;
}
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
{
static NSString *id = @ "Cell";
Customcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:id Forindexpath:indexpath];
Cell.imageView.image = [UIImage Imagenamed:self.photos[indexpath.item]];
return cell;
}
5. Modify the storyboard Collectionviewcell, such as modify the background color, put a picture, set the constraints of the picture, the most important thing is to modify the reuse tag as a cell, that is, the code is written in accordance with the
6. Create a new custom Collectionviewcell class, add the export, that is, with the storyboard control to do the connection
@interface Customcell:uicollectionviewcell
@property (Weak, nonatomic) Iboutlet Uiimageview *imageview;
@end
7. Operation Effect
8. Compared with pure code creation CollectionView, the following steps are saved
[CollectionView Registerclass:[customcell class] forcellwithreuseidentifier:id]; //Register cell
Uicollectionview *collectionview = [[Uicollectionview alloc] InitWithFrame:self.view.bounds collectionviewlayout:[[ Uicollectionviewflowlayout alloc] [init]]; //Initialize, specify the layout
There is also a way to construct a custom cell
-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Uiimageview *imageview = [[Uiimageview alloc]init];
Imageview.frame = CGRectMake (5, 5, 40, 40);
[Self addsubview:imageview];
_imagev = ImageView;
}
return self;
}
Note: Storyboard dragged out of the Collectionviewcontroller default is the flow layout
Similarities and differences of four registration methods
-(void) RegisterClass: (Class) Cellclass forcellwithreuseidentifier: (NSString *) identifier;
-(void) registernib: (uinib *) nib Forcellwithreuseidentifier: (NSString *) identifier;
-(void) RegisterClass: (Class) Viewclass forsupplementaryviewofkind: (NSString *) Elementkind Withreuseidentifier: ( NSString *) identifier;
-(void) registernib: (uinib *) nib Forsupplementaryviewofkind: (NSString *) kind withreuseidentifier: (NSString *) Identifier
Cell is the 1th type of code creation;
If the cell is created by Xib, use the 2nd type;
The following 2 are used to register a supplemental view, a supplemental view similar to TableView's sectionheader,sectionfooter, which distinguishes the supplemental view header from the Elementkind parameter or complements the end of the view. tail uicollectionelementkindsectionfooter head uicollectionelementkindsectionheader
Use of IOS Uicollectionview (created with storyboard and xib)