Uicollectionview Getting Started--using the system Uicollectionviewflowlayout layout class

Source: Internet
Author: User
Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://rainbownight.blog.51cto.com/1336585/1323780

Initialization

Initialize the layout class (subclass of Uicollectionviewlayout)
Uicollectionviewflowlayout *FL = [[Uicollectionviewflowlayout alloc]init];

Initialize CollectionView
Self.collectionview = [[Uicollectionview alloc]initwithframe:cgrectzero COLLECTIONVIEWLAYOUT:FL];

Set up Proxy
Self.collectionView.delegate = self;
Self.collectionView.dataSource = self;

--------------------

protocols that need to be implemented:
Uicollectionviewdatasource, Uicollectionviewdelegateflowlayout
Ps:uicollectionviewdelegateflowlayout is Uicollectionviewdelegate's sub-protocol.

--------------------

Registers the corresponding Uicollectionviewcell subclass to CollectionView to be used to extract and display from the queue
-(void) RegisterClass: (Class) Cellclass forcellwithreuseidentifier: (NSString *) identifier;
PS: If it is created with nib, use the following function to register.
-(void) registernib: (uinib *) nib Forcellwithreuseidentifier: (NSString *) identifier;

If you need to display the Headerview or Footerview for each section, you also need to register the corresponding subclass of Uicollectionreusableview to CollectionView
Elementkind is the identifier of the header or footer, There are only two ways to set Uicollectionelementkindsectionheader and Uicollectionelementkindsectionfooter
-(void) RegisterClass: (Class) Viewclass forsupplementaryviewofkind: (NSString *) Elementkind Withreuseidentifier: ( NSString *) identifier;
PS: If it is created with nib, use the following function to register.
-(void) registernib: (uinib *) nib Forsupplementaryviewofkind: (NSString *) kind withreuseidentifier: (NSString *) Identifier

--------------------

Functions that implement the Protocol:
Like UITableView's DataSource and delegate, they can take their own interpretations.

DataSource:

How many cells are there in each group?
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section;

Define and return each cell
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath;

How many groups are there in the CollectionView?
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView;

Define and return each Headerview or Footerview
-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath;

One thing to note when using this method is that the method will not be called if the layout does not have a size set for Headerview or Footerview (the default size is Cgsizezero). So if you need to display headers or footer, you need to set the size manually.
You can control the size globally by setting the Headerreferencesize and Footerreferencesize properties of the uicollectionviewflowlayout. or by overloading the following proxy methods to set the
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Referencesizeforheaderinsection: (nsinteger) Section;
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Referencesizeforfooterinsection: (nsinteger) Section;

Delegate:

The size of each cell
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath;

Set the cell boundaries for each group, as shown below
-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Insetforsectionatindex: (nsinteger) Section;



Minimum line spacing for cells
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) Section;

Minimum column spacing for cells
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) Section;

Called when Cell is selected
-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath;

Called when cell is reversed (takes effect when multiple choices are selected)
-(void) CollectionView: (Uicollectionview *) CollectionView Diddeselectitematindexpath: (Nsindexpath *) IndexPath;

This article is from the "rainbownight" blog, make sure to keep this source http://rainbownight.blog.51cto.com/1336585/1323780

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.