Simple use of iOS basics _ Uicollectionview

Source: Internet
Author: User

There are two ways to use uicollectionview like table view

One is inherit Uicollectionviewcontroller, this controller will bring a uicollectionview;

The other is to create a uiconllectionview view that is placed inside the normal Uiviewcontroller.

We use the second type of


first declare a declaration first Reuse Markers and implement a delegate

#define _cell @"Acell"

@interfaceyxpviewcontroller () <Uicollectionviewdatasource, uicollectionviewdelegate , Uicollectionviewdelegateflowlayout >


Then initialize the Uicollectionview

-(void) Initcollectionview

{

//Instantiate a layer first

uicollectionviewflowlayout *layout=[[uicollectionviewflowlayoutalloc] init];

//Create one-screen view size

uicollectionview *collectionview=[[uicollectionviewalloc] initwithframe: self. View. bounds collectionviewlayout: layout];

[CollectionViewregisterclass: [Uicollectionviewcellclass] Forcellwithreuseidentifier:_cell];

CollectionView. backgroundcolor=[uicolorwhitecolor];

CollectionView. delegate=self;

CollectionView. DataSource=self;

[Self. View addsubview: CollectionView];

}


Implementing Proxy methods

#pragma Mark--uicollectionviewdatasource

Defines the number of Uicollectionviewcell to show

-(Nsinteger) CollectionView: (uicollectionview *) CollectionView numberofitemsinsection: ( Nsinteger) Section

{

return ;

}

Define the number of sections to show

-(Nsinteger) Numberofsectionsincollectionview: (uicollectionview *) CollectionView

{

return 1;

}

What each Uicollectionview shows

-(uicollectionviewcell *) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: (nsindexpath *) Indexpath

{

Uicollectionviewcell * cell = [CollectionViewdequeuereusablecellwithreuseidentifier :_cellforindexpath: Indexpath];

cell.BackgroundColor = [UicolorColorwithred:((Arc4random()%255)/255.0)Green:((Arc4random()%255)/255.0)Blue:((Arc4random()%255)/255.0)Alpha:1.0f];

return cell;

}


#pragma Mark--uicollectionviewdelegate

method to invoke when Uicollectionview is selected

-(void) CollectionView: (uicollectionview *) CollectionView Didselectitematindexpath: ( Nsindexpath *) Indexpath

{

Uicollectionviewcell * cell = (uicollectionviewcell *) [CollectionViewCellforitematindexpath : Indexpath];

cell.BackgroundColor = [UicolorColorwithred:((Arc4random()%255)/255.0)Green:((Arc4random()%255)/255.0)Blue:((Arc4random()%255)/255.0)Alpha:1.0f];

}

Returns whether this Uicollectionviewcell can be selected

-(BOOL) CollectionView: (uicollectionview *) CollectionView Shouldselectitematindexpath: ( Nsindexpath *) Indexpath

{

return YES;

}



#pragma Mark--uicollectionviewdelegateflowlayout

Define the size of each Uicollectionview

-(cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (nsindexpath *) Indexpath

{

return Cgsizemake (a);

}

Define margins for each Uicollectionview

-(uiedgeinsets) CollectionView: (uicollectionview *) CollectionView layout: ( Uicollectionviewlayout *) collectionviewlayout insetforsectionatindex: (nsinteger) Section

{

return Uiedgeinsetsmake (Ten, 10 ,ten);

}


such a simple uicollection view is done

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.