IOS Custom Colletionview (pure code)

Source: Internet
Author: User

Everyone said that Colletionview and Uitabbleview are brothers, and Colletionview is out after iOS 6, Colletionview and Uitabbleview they are really brothers, but used when you encounter a lot of pits.
Like what:
Uicollectionview *colletionview = [[Uicollectionview alloc]init]; initialize a colletionview, if you do, you fall into the pit. This is the official document for others.
So!!!! You're going to blow it off like that. That's all you can do.

 Uicollectionviewflowlayout*grid =[[UicollectionviewflowlayoutALLOC] init]; Grid.itemsize =Cgsizemake( the, the);//Set the size of the colletionview Grid.sectioninset =Uiedgeinsetsmake(10.0,Ten,Ten,Ten);Uicollectionview*colletionview = [[UicollectionviewAllocinitWithFrame: Self. view.framecollectionviewlayout:Grid];colletionview.delegate = Self; Colletionview.datasource = Self; [Colletionviewregisterclass:[Photocell class] forcellwithreuseidentifier: @ "Simplecell"];//This must add not to add your cell init will not be called Oh!! 

OK is successfully instantiating a colletionview it's like its brother. You need to set up a proxy and set up a data source. It has been set up at the time of instantiation.
Now go and implement his proxy and customize a Colletionviewcell
Custom cell is going to customize a cell class to inherit Uicollectionviewcell
. h

#import <UIKit/UIKit.h>@interface Cell : UICollectionViewCell@property (nonatomic,strong)UIImageView *image;@end

. m

 @implementation Cell -(ID) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self)        {//Change to our custom selected background view             Self. Image= [[UiimageviewAlloc]init]; [ Self. ImageSetframe:cgrectmake ( Self. Contentview. Frame. Size. Width/2+ -+5, -5, -, -)]; [ Self. Imagesetbackgroundcolor:[UicolorRedcolor]]; Self. Image. Layer. Cornerradius= Self. Image. Frame. Size. Width/2;//Set the border of a layer to rounded corners             Self. Image. Layer. Maskstobounds=YES;//Hide Border[ SelfAddsubview: Self. Image]; }return  Self;}@end
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 1;} -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (Nsinteger) section{return Ten;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath*) indexpath{Static NSString*cellidentifier = @"Simplecell"; Cell *cell = (cell *) [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath];if(Cell = =Nil) {cell = [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath]; }returnCell;} - (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath*) indexpath{//--Single Event}

IOS Custom Colletionview (pure code)

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.