Use of iOS Uicollectionview
Last Update:2015-07-31
Source: Internet
Author: User
<span id="Label3"></p><p><p>First document engineering obeys <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> two agreements</p></p><p><p>Then look at the code</p></p><p>Uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc] init];<br>[flowlayout setscrolldirection:uicollectionviewscrolldirectionvertical];<br>Flowlayout.headerreferencesize = Cgsizemake (self.view.frame.size.width, 60);<br><br>Uicollectionview *collectionview = [[uicollectionview alloc] initwithframe:cgrectmake (0, 0, self.view.frame.size.width, Self.view.frame.size.height) collectionviewlayout:flowlayout];<br>Collectionview.datasource = self;<br>Collectionview.delegate = self;<br>[collectionview Registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@ "GradientCell"];<br>[collectionview Registerclass:[uicollectionreusableview class] forsupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "header"];<br>[self.view addsubview:collectionview];<br><br><br></p><p><p>To Initialize: Uicollectionviewflowlayout</p></p><p><p>After initialization: Uicollectionview but sets the initwithframe:collectionviewlayout at initialization time:</p></p><p><p><br></p></p><p><p><br></p></p><p><p>At last</p></p><p>-(nsinteger) collectionview: (uicollectionview *) collectionview numberofitemsinsection: (NSInteger) section<br>{<br>Return 10;<br>}<br><br>Define the number of sections to show<br>-(nsinteger) numberofsectionsincollectionview: (uicollectionview *) CollectionView<br>{<br>Return 1;<br>}<br><br>What each Uicollectionview shows<br>-(uicollectionviewcell *) collectionview: (uicollectionview *) collectionview cellforitematindexpath: (NSIndexPath *) Indexpath<br>{<br>Static NSString * Cellidentifier = @ "gradientcell";<br>Uicollectionviewcell * cell = [collectionview Dequeuereusablecellwithreuseidentifier:cellidentifier forIndexPath: indexpath];<br><br>Cell.backgroundcolor = [uicolor colorwithred: ((* indexpath.row)/255.0) green: ((* indexpath.row)/255.0) blue: ((30 * Indexpath.row)/255.0) alpha:1.0f];<br>Return cell;<br>}<br><br>-(uicollectionreusableview *) collectionview: (uicollectionview *) CollectionView viewforsupplementaryelementofkind :(nsstring *) Kind atindexpath: (nsindexpath *) indexpath<br>{<br>Uicollectionreusableview *headerview = [collectionview dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "header" forindexpath:indexpath];<br>UILabel *label = [[UILabel alloc] initwithframe:cgrectmake (0, 0, self.view.frame.size.width, 50)];<br>Label.backgroundcolor = [uicolor lightgraycolor];<br>[headerview addsubview:label];<br>Return headerview;<br>}<br><br><br>#pragma mark--uicollectionviewdelegateflowlayout<br><br>Define the size of each Uicollectionview<br>-(cgsize) collectionview: (uicollectionview *) collectionview layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (nsindexpath *) Indexpath<br>{<br>return Cgsizemake (80, 100);<br>}<br><br>Define margin for each Uicollectionview<br>-(uiedgeinsets) collectionview: (uicollectionview *) collectionview layout: (uicollectionviewlayout *) Collectionviewlayout insetforsectionatindex: (nsinteger) Section<br>{<br>return uiedgeinsetsmake (5, 5, 5, 5);<br>}<br><br>#pragma mark--uicollectionviewdelegate<br><br>method to invoke when Uicollectionview is selected<br>-(void) collectionview: (uicollectionview *) collectionview didselectitematindexpath: (nsindexpath *) IndexPath<br>{<br>Uicollectionviewcell * cell = (uicollectionviewcell *) [collectionview cellforitematindexpath:indexpath];<br>Cell.backgroundcolor = [uicolor whitecolor];<br>}<br><br>Returns whether this Uicollectionview can be selected<br>-(BOOL) collectionview: (uicollectionview *) collectionview shouldselectitematindexpath: (nsindexpath *) IndexPath<br>{<br>Return YES;<br>}<br><br>Just a simple understanding, you can copy the code into your project, run under the test, you search the relevant blog, improve<br></p><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Use of iOS Uicollectionview</p></p></span>