Knowledge points
Creation of 1.UICollectionView
2. Common proxy methods for Uicollectionview
3. Uicollectionview related xib operation
================================
Creation of Uicollectionview
1.UICollectionViewLayout
Function: An abstract class that controls CollectionView layout
Note: This class is generally not used directly, because many methods of this class are not implemented, only a number of properties and methods are specified, self-defined layouts need to create a class to inherit Uicollectionviewlayout, and then set a custom layout
2.UICollectionViewFlowLayout
Function: A waterfall flow layout written by the system
Use a waterfall flow layout written by the system
Uicollectionviewflowlayout *layout = [Uicollectionviewflowlayout new];
/*
Uicollectionviewscrolldirectionvertical,
Uicollectionviewscrolldirectionhorizontal
*/
Set the direction of the slide
Layout.scrolldirection = uicollectionviewscrolldirectionvertical;
Sets the minimum spacing between item (when sliding vertically, indicating the horizontal spacing, horizontal sliding, indicating vertical spacing)
layout.minimuminteritemspacing = 35;
Sets the minimum spacing between item (when vertical sliding, indicates the longitudinal spacing, horizontal sliding, indicating the horizontal spacing)
layout.minimumlinespacing = 10;
3.-(Instancetype) initWithFrame: (CGRect) frame collectionviewlayout: (uicollectionviewlayout *) layout;
Role: To create a uicollectionview, you must provide a uicollectionviewlayout
Instantiate a Uicollectionview
Uicollectionview *collectionview = [[Uicollectionview alloc] initWithFrame:self.view.bounds collectionviewlayout: Layout];
4. Protocol Agent
1) Uicollectionviewdatasource
2) Uicollectionviewdelegateflowlayout
================================
Common proxy methods for Uicollectionview
Proxy methods for #pragma mark-uicollectionview
1.-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView
Function: Returns the number of groups
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section{
return 10;
}
2.-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section;
Function: Returns the number of elements
Returns the number of elements in each group
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
{
return 10;
}
3.-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath * ) Indexpath;
Function: Returns the Uicollectionviewcell used by the element
Returns the Uicollectionviewcell object used by each element
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
{
To re-use the queue to find the cell
Uicollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:@ "cell" ForIndexPath:indexPath] ;
Change the background color
Cell.backgroundcolor = [Uicolor Redcolor];
return cell;
}
4.-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath;
Function: Returns the cgsize size of the element
Modify the width and height of each item
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{
Return Cgsizemake (100, 100);
}
5.-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) Section;
Function: Returns the minimum allowable spacing between elements, or vertical distance if horizontal slide, or horizontal distance if sliding vertically
Effect with layout.minimuminteritemspeacing, two choice one
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Minimuminteritemspacingforsectionatindex: (Nsinteger) section{
}
6.-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) Section;
Function: Returns the minimum allowable spacing between elements, or horizontal sliding, which represents the horizontal distance, or vertical distance if sliding vertically
Effect with layout.minimumlinespacing, two choice one
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Minimumlinespacingforsectionatindex: (Nsinteger) section{
}
7.-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) Section
Function: Controls the distance (top, left, bottom, right) of a set of views and screen boundaries
Returns the distance (top, left, bottom, right) of each set of elements to the screen 4 boundaries
-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (Nsinteger) section{
Create a uiedgeinsets structure
Return Uiedgeinsetsmake (10, 10, 10, 10);
}
8.-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout referencesizeforheaderinsection: (nsinteger) Section
Function: Returns the head view of the cgsize, if the horizontal slide, then the width is valid, if the vertical sliding, only highly effective
Returns the width and height of the head view
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Referencesizeforheaderinsection: (Nsinteger) section{
Note: If you are sliding vertically, it is only highly effective, and if you are sliding horizontally, only the width is valid
Return Cgsizemake (50, 50);
}
9.-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (NSString *) kind Atindexpath: (Nsindexpath *) Indexpath
Function: Return to head view
Returns the Uicollectionreusableview object used by the head and tail views
-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{
Since the instantiation of the head and tail view requires this method to be called, it is necessary to determine by kind whether a head view or a trailer view is generated
if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {
Represents the need to create a head view
Create a head view in reusable form
Uicollectionreusableview *headerview = [CollectionView dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "header" Forindexpath:indexpath];
Change the background color
Headerview.backgroundcolor = [Uicolor Greencolor];
return headerview;
}
return nil;
}
10.-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath;
Function: Select an element
Select an item
-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexPath{
NSLog (@ "%ld section%ld", Indexpath.section,indexpath.item);
}
iOS Development-ui (iii) Collection