I. Uicollectionviewdatasource
1. Ways to return the number of sections
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView { return 5 ;}
2. Methods to return the number of cells in each section
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section { return;}
3. Select the cell used in the CollectionView
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView *) Indexpath { // using the cell reuse identifier to get cell Collectionviewcell *cell = [CollectionView Dequeuereusablecellwithreuseidentifier:reuseidentifier Forindexpath:indexpath]; return cell;}
4. How to register Uicollectionreusableview.
uinib *headernib = [uinib nibwithnibname:@"Collectionheaderreusableview"bundle: [NSBundle Mainbundle]]; //Register to Reuse view[Self.collectionview registernib:headernib Forsupplementaryviewofkind:uicollectionelementkindsectionhea Der Withreuseidentifier:@"Collectionheaderreusableview"]; //Register Footerviewuinib *footernib = [uinib nibwithnibname:@"Collectionfooterreusableview"bundle:[NSBundle Mainbundle]]; [Self.collectionview registernib:footernib Forsupplementaryviewofkind:uicollectionelementkindsectionfooter Withreuseidentifier:@"Collectionfooterreusableview"];
5. Settings in Uicollectionviewdatasource supplementary View
-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView viewforsupplementaryelementofkind: (NSString*) Kind Atindexpath: (Nsindexpath*) indexpath{//Set Sectionheader if([Kind Isequaltostring:uicollectionelementkindsectionheader]) {Uicollectionreusableview*view = [CollectionView dequeuereusablesupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:@"Collectionheaderreusableview"Forindexpath:indexpath]; returnview; } //Set SectionfooterUicollectionreusableview *view = [CollectionView dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionfooter Withreuseidentifier:@"Collectionfooterreusableview"Forindexpath:indexpath]; returnview; }
Two. Uicollectionviewdelegateflowlayout
1.Cell Custom Size
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout *) indexpath{ if0) { return Cgsizemake (); } return Cgsizemake ();}
2. Change the upper and lower left and Right margins of section--uiedgeinsetsmake
-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout* ) collectionviewlayout insetforsectionatindex: (nsinteger) section{ if0) { return Uiedgeinsetsmake () ; return Uiedgeinsetsmake (0000);}
3. The upper and lower margins of each cell
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayoutminimumlinespacingforsectionatindex: (nsinteger) section{ if0 ) { return5.0f; } return 20.0f ;}
4. Set the left and right margins of the cell
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayoutminimuminteritemspacingforsectionatindex: (nsinteger) section{ if0 ) { return5.0f; } return 20.0f ;}
5. Set the size of the header view and footer view
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayoutreferencesizeforheaderinsection: (nsinteger) section{ return cgsizemake ( $ - );} -(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayoutreferencesizeforfooterinsection: (nsinteger) section{ return cgsizemake (a);}
Three. Uicollectionviewdelegate
1. Set cell to highlight
-(BOOL) CollectionView: (Uicollectionview **) indexpath{ return YES; }
The following method is called back when the 2.Cell has changed from non-highlight to highlight and from highlight to non-highlighted state
-(void) CollectionView: (Uicollectionview **) indexpath{ [self Changehighlightcellwithindexpath:indexpath];} -(void) CollectionView: (Uicollectionview **) indexpath{ [self Changehighlightcellwithindexpath:indexpath];}
3. Set whether the cell is optional
-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath { return YES;}
4.Cell Support Multi-Select
Self.collectionView.allowsMultipleSelection = YES;
5. Need to support canceling cell multiple selection in multi-select mode
-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shoulddeselectitematindexpath: (Nsindexpath *) indexpath{ return YES;
6.Cell will appear, after the cell appears,Supplementary view will appear and supplementary view has appeared
/** * Call this method when the cell is about to appear*/- (void) CollectionView: (Uicollectionview *) CollectionView Willdisplaycell: (Uicollectionviewcell *) cell ForItemAtIndexPath :(Nsindexpath *) Indexpath Ns_available_ios (8_0) {NSLog (@"the%ld cell on the first%ld section will appear", Indexpath.section, Indexpath.row);}/** * Call this method after the cell appears*/- (void) CollectionView: (Uicollectionview *) CollectionView Didenddisplayingcell: (Uicollectionviewcell *) cell Foritematindexpath: (Nsindexpath *) indexpath{NSLog (@"the%ld cell on section%ld has already appeared", Indexpath.section, Indexpath.row);}/** * Headerview or Footerview will appear when the method is called*/- (void) CollectionView: (Uicollectionview *) CollectionView Willdisplaysupplementaryview: (Uicollectionreusableview *) view Forelementkind: (NSString *) elementkind Atindexpath: (Nsindexpath *) Indexpath Ns_available_ios (8_0) {NSLog (@"the%ld extension view on the first%ld section will appear", Indexpath.section, Indexpath.row); }/** * Headerview or Footerview occurs after the method is called*/- (void) CollectionView: (Uicollectionview *) CollectionView Didenddisplayingsupplementaryview: (UICollectionReusableView *) View Forelementofkind: (NSString *) elementkind Atindexpath: (Nsindexpath *) indexpath{NSLog (@"The first%ld extension view on section%ld has already appeared", Indexpath.section, Indexpath.row); }
Ios-uicollectionviewcontroller protocol and callbacks