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