<span id="Label3"></p><p><p>Uicollectionview is created in the same way that UITableView is created</p></p><p><p>first, Create a subclass that inherits from the Uicollectionview</p></p><p><p>Then set some properties in the initialization method</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(id) initwithframe: (cgrect) frame{ uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc] init]; flowlayout.minimuminteritemspacing = 0; Column spacing flowlayout.minimumlinespacing = 0; Line spacing Self = [super initwithframe:frame collectionviewlayout:flowlayout]; If (self) { //hide slider self.showshorizontalscrollindicator = NO; Self.showsverticalscrollindicator = NO; Set proxy self.delegate = self; Self.datasource = self; Set background color (default black) self.backgroundcolor = [uicolor whitecolor]; Register cell [self registerclass:[ysstudentstatuscell class] forcellwithreuseidentifier:identify]; } Return self;}</pre></pre><br>The CollectionView protocol method is basically the same as the tableview, the main difference is the creation of the cell and the creation of the head VIEW.<p><p></p></p><p><p>Look at cell creation First.</p></p><p><p></p></p><pre name="code" class="objc"><p class= "p1" ></p><p class= "p1" >//create cell</p><p class= "p1" >-(<span class= "s1" > uicollectionviewcell</span> *) collectionview: (<span class= "s1" >UICollectionView</span> *) CollectionView cellforitematindexpath: (<span class= "s1" >NSIndexPath</span> *) indexpath</p><p class= "p1" >{</p><p class= "p1" > <span class= "s2" >ModelStudent</span> *model = <span class= "s3" >self</span>.<span class= "s2" >dataarray</span>[indexpath.<span class= " S1 ">item</span>];</p><p class=" p1 ">/sub-class cell</p><p class=" p2 "><span class=" S4 "> </span><span class=" S2 ">ysstudentstatuscell</span><span class=" S4 "> * Cell = [collectionview </span>dequeuereusablecellwithreuseidentifier<span class= "s4" >:</span> <span class= "s2" >identify</span><span class= "s4" > </span>forindexpath≪span class= "s4" >:indexpath];</span></p><p class= "p1" > [cell <span class= "s5" >configurewithmodel</span>:model.<span class= "s2" >stuuserinfo</span>];</p><p class = "p1" > <span class= "s3" >return</span> cell;</p><p class= "p1" >}</p></pre><p><p></p></p>The initialization method for the subclass cell creation of CollectionView is as follows, I do not execute with Init<p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(id) initwithframe: (cgrect) frame{self = [super initwithframe:frame]; If (self) { [self initview]; } Return self;}</pre></pre><br>CollectionView head view of the creation, first need to register the head view, registered demo I generally with the cell register written in a piece<p><p></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">Collection Header View Registration [self Registerclass:[uicollectionreusableview class] forsupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "identifierhead"];</pre></pre>To create a protocol method for the head view<p><p></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">Group header View Creation-(uicollectionreusableview *) collectionview: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind atindexpath: (nsindexpath *) indexpath{ Uicollectionreusableview *headview = [collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@ "identifierhead" forindexpath:indexpath]; Headview.backgroundcolor = [uicolor bluecolor]; Return headview;}</pre></pre><p><p></p></p><p class="p1"><p class="p1"><br></p></p>Also explain the use of FlowLayout<p><p></p></p><p class="p1"><p class="p1">FlowLayout can be said to be CollectionView layout properties, set different flowlayout, can be loaded out of different CollectionView layout style</p></p><br><p><p><br></p></p><p><p>Ios:uicollectionview creation of the Sub-Class</p></p></span>
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