The Uicollectionviewcontroller of iOS------------the way to salvation

Source: Internet
Author: User

Uicollectionviewcolltroller is a view of the data set used in iOS, powerful, and can be achieved visually rich, this essay author demonstrates the Uicollectionviewconlltroller simple use, To those who are on the same path of programming.

First look at Uicolletionviewcontroller various effects (source Baidu)

Uicollectionviewcontroller has three very important protocol uicollectionviewdelegate, Uicollectionviewdatasource,uicollectionviewdelegateflowlayout

Their roles are data sources, event proxies, and view layouts. Let me tell you about the usage of these three protocols in the demo again.

Begin

Uicollectionviewdatasource three important methods of 1,numberofsectionsincollectionview. 2, (Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section. 3, (Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section

First, create a new project, and then delete the default new Stroryboard with view, configuration item Settings main interface is empty, add code in Appdelegate

    UIWindow *window = [[UIWindow alloc]initwithframe:[[uiscreen Mainscreen] bounds]];    [Window makekeyandvisible];     = window;         *flowlayout = [[uicollectionviewflowlayout alloc]init];     *CC = [[Yyhcollectionviewcontroller alloc]initwithcollectionviewlayout:flowlayout];     = CC;

Note that for Alloc uicollectionviewcontroller you must give an Uiconllectionviewflowlayout object, otherwise you will get an error.

Second, although UICOLLECTIONVIEWCONTROLELR and Uitableviewcontrller are also used to display data, but the user-friendliness is much worse. Because there are no default controls in the Cell,headview,footview, in other words, all the controls need to be defined by yourself.

Uicollectionviewcontroller custom cell with Headview,footview and Uitableviewcontrller big difference, but you just need to remember a method Initframe: (CGRect) Frame, all of your custom controls are to be fetched from this.

Create a new cell to inherit the Uicollectionview, and then make the necessary output connections.

To load the Initframe function in a. m file

-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) {Nsarray*array = [[NSBundle mainbundle] loadnibnamed:@"Yyhcollectionviewcell"owner:self Options:nil]; if(Array.count <1)            returnNil; if(! [[Array Firstobject] Iskindofclass:[uicollectionviewcellclass]])            returnNil; Self=[Array firstobject]; }    returnSelf ;}

The custom cell has been completed so far.

Third, show the data

#pragma mark <uicollectionviewdatasource>//returns the number of Secion-(Nsinteger) Numberofsectionsincollectionview: ( Uicollectionview *) CollectionView {    return self.radios.count;} Returns the number of cells under each section-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: ( Nsinteger) Section {    radiolist *list = self.radios[section];    return list.radios.count;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {    Yyhcollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier: Reuseidentifier Forindexpath:indexpath];    Radiolist *list = self.radios[indexpath.section];    Radio *radio = List.radios[indexpath.row];    Cell.imageView.image = [UIImage imageNamed:radio.imgUrl];    Cell.descLabel.text = Radio.fmname;        return cell;}

The results are as follows:

The result is different from our custom, we are ideally a uiimageview, a uilabel. But now the show is only Uiimageview.

To find the problem is to rely on their own, programming is a constant search for problems, and then solve the problem. Repeatedly find, I suspect is the cell frame problem, output fram view

NSLog (@"%f", Cell.frame.size.width); NSLog (@"%f", cell.frame.size.height); .- on- +  +: -:46.040uicollectionviewcontrollerdemo[3260:90515]50.000000 .- on- +  +: -:46.041uicollectionviewcontrollerdemo[3260:90515]50.000000

Found with xib size does not match, draw result frame problem (xib.size = {75,90}

From here we draw uicollectionviewflowlayout

Uicollectionviewflowlayout

Cell Size-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath;//cell void-(Uiedgeinsets) CollectionView: ( Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Insetforsectionatindex: ( Nsinteger) interval between Section;//cell-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: ( uicollectionviewlayout*) collectionviewlayout Minimumlinespacingforsectionatindex: (NSInteger) section;// Interval between secion-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) Section;//reuseheadview size-(CGSize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Referencesizeforheaderinsection: (nsinteger) section;//reusefoothead size-(cgsize) CollectionView: (UICollectionView * ) CollectionView layout: (uicollectionviewlayout*) COLlectionviewlayout referencesizeforfooterinsection: (nsinteger) Section; 

The above methods are used for layout uicollectionview.

Here I choose

-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath; To solve the problem that my cell display is incomplete
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{    cgsize size = {75,90};    return size;}

Results as scheduled:

But in fact we need to show the case of grouping, then we will implement the case of grouping

Four, implement grouping. In fact, Uicollectionviewcontroller group is the key is section>1, in fact, we have grouped, in fact, the UI is not reasonable display, if it is reasonable to display it? That's adding reuseheadview.

New File Inheritance Uicollectionreuseview

Also need to be in the. M file Overloading Initframe method

-(Instancetype) initWithFrame: (cgrect) frame{    if (self = [super Initwithframe:frame])    {        Nsarray *arrary = [ [NSBundle Mainbundle] loadnibnamed:@ "Radiocollectionreusableview" owner:self Options:nil];        if (Arrary.count < 1)            return nil;        if (![ [Arrary Firstobject] Iskindofclass:[uicollectionreusableview class]])            return nil;        Self = [arrary firstobject];    }    return self;}

The custom Headview is done here.

Five, show Headview

Add code

//Implement Headview-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{Radiocollectionreusableview*headview =[CollectionView dequeuereusablesupplementaryviewofkind:kind Withreuseidentifier:reuseidentifierforheadview    Forindexpath:indexpath]; if([Kind Iskindofclass:[uicollectionelementkindsectionheaderclass]]) {radiolist*list =Self.radios[indexpath.section]; HeadView.descLabel.text=List.name; }    returnHeadview;}//Headview Size-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout referencesizeforheaderinsection: (nsinteger) section{cgsize currentsize=[[UIScreen mainscreen] bounds].size; Cgsize headsize= {Currentsize.width, $}; returnheadsize;}

Results show:

Supplemental: Custom cell and Headview are required to register with Collectionviewcontroller

    class] forcellwithreuseidentifier:reuseidentifier];     class  ] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier: Reuseidentifierforheadview];   

Uicollectionviewcontrollerdelegate, simply describe the function of the method inside

Whether the cell should be highlighted-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldhighlightitematindexpath: (Nsindexpath *) Whether Indexpath;//cell can be selected-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: ( Nsindexpath *) Indexpath;//cell can be unchecked-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shoulddeselectitematindexpath: (Nsindexpath *) Indexpath; Cell selected when called-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) Indexpath;//cell is unchecked call-(void) CollectionView: (Uicollectionview *) CollectionView Diddeselectitematindexpath: ( Nsindexpath *) Indexpath;

This is the end of the point, the shortcomings of the hope that more advice

Incidental demo:http://download.csdn.net/detail/q13432502528/9410574

The Uicollectionviewcontroller of iOS------------the way to salvation

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.