Uicollectionview Layout Features

Source: Internet
Author: User

Uiconllectionview and UITableView are similar, but also display data, but unlike the uitableview of the rules of the layout, Uicollectionview can achieve irregular layout, that is, waterfall flow.

Create Uicollectionview

Uicollectionview *collectionview = [[Uicollectionview alloc] initwithframe:[[uiscreen mainscreen] bounds] Collectionviewlayout:layout];

The creation of the collection view, you must specify the layout, if there is no layout, can not show anything, that is, layout.

// Create a layout object that uses the system layout class Uicollectinviewflowlayout    Uicollectionviewflowlayout *layout = [[Uicollectionviewflowlayout alloc] init];

Because it is a system layout class, it is also a rule, but can be customized FlowLayout, can be based on their own needs, to create an irregular grid.

Individual layout details can be set separately

//set the minimum line spacingLayout.minimumlinespacing = -; //to set the spacing between item and itemLayout.minimuminteritemspacing =Ten; //partition interval for collection views//four values on the left lower rightLayout.sectioninset = Uiedgeinsetsmake ( -,Ten,Ten,Ten); //set the slide direction of the collection ViewLayout.scrolldirection = uicollectionviewscrolldirectionvertical;//downwardLayout.scrolldirection = Uicollectionviewscrolldirectionhorizontal;//RightCGFloat totalWidth =Self.view.frame.size.width; //set the size of each item//layout.itemsize = Cgsizemake ((totalWidth-40)/3, +);

Of course, after signing the agreement can also be set through the method

When it comes to protocols, the protocol is divided into two parts, the data source protocol Uicollectionviewdelegatesource and the proxy protocol uicollectionviewdelegate

because of the layout, it will also be signed by the layout agreement Uicollectionviewdelegateflowlayout

-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{returnCgsizemake (Kwidth- +) /3, -);}-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Insetforsectionatindex: (nsinteger) section{returnUiedgeinsetsmake (0,0,0,0);}-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) section{return  -;}-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section{return  -;} 

Uicollectionviewdatasource and UITableView, there are two methods that must be implemented.

//Display number-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return  -;}//What each cell displays-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Mycollectionviewcell*cell = [CollectionView dequeuereusablecellwithreuseidentifier:@"Reuse"Forindexpath:indexpath]; Cell.contentView.backgroundColor= [Uicolor colorwithred:kcolor green:kcolor Blue:kcolor Alpha:1.0]; Cell.numberLabel.text= [NSString stringWithFormat:@"%ld", Indexpath.row]; returncell;}

My side of the cell displays a label. Customize the cell to format the label.

As with UITableView, each item can be clicked, triggering the method

// the method to trigger after item click -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath :(Nsindexpath *) indexpath{    NSLog (@ " number of partitions%ld, number of rows%ld", Indexpath.section, Indexpath.row);}

It is important to note that the collection view is not a collection view, like a table view, and if you want to display the content, you must register the cell

// Collection View If you want to display content, you must register    the cell class] Forcellwithreuseidentifier:@ "reuse"];

Attention:

The irregular layout of the collection view is often used more frequently, because each spatial layout is not necessarily a rule, and there are differences, through custom FlowLayout. To show the different layouts

Uicollectionview Layout Features

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.