Collection View Code

Source: Internet
Author: User

#pragma mark-Set up a custom view

-(void) Loadview

{

Self.rootview = [[[Rootview alloc] Initwithframe:[uiscreen mainscreen].bounds] autorelease];

_rootview.backgroundcolor = [Uicolor Redcolor];

Self.view = _rootview;

}

-(void) Viewdidload {

[Super Viewdidload];

Set up data sources and proxies

Self.rootView.collectionView.dataSource = self;

Self.rootView.collectionView.delegate = self;

Collectionviewcell can only use Storyboard (xib) or register with

[Self.rootView.collectionView Registerclass:[mycollectionviewcell class] forcellwithreuseidentifier:@ "identifier" ];

Register head and tail areas

[Self.rootView.collectionView Registerclass:[uicollectionreusableview class] Forsupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "header"];

[Self.rootView.collectionView Registerclass:[uicollectionreusableview class] Forsupplementaryviewofkind: Uicollectionelementkindsectionfooter withreuseidentifier:@ "Footer"];

}

#pragma mark-uicollectionviewdatasource Methods

#pragma mark to set the number of partitions

-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView

{

return 3;

}

#pragma mark sets the number of each partition

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section

{

return 9;

}

#pragma mark sets what is displayed on each item

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath

{

1. Directly to the reuse queue to find, if not, will automatically help us create

Mycollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:@ "identifier" ForIndexPath: Indexpath];

2. Use

Cell.backgroundcolor = [Uicolor Cyancolor];

Cell.label.text = [NSString stringwithformat:@ "%ld%ld", Indexpath.section, Indexpath.row];

3. Return

return cell;

}

#pragma mark handles the head and tail areas of each partition

-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath

{

For the Collection View (Uicollectionview), the header and tail areas use the Uicollectionreusableview class instead of the UIView, and the reuse mechanism is also used when setting up

Use kind to determine whether the current display header or tail area

if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {

Head area

Uicollectionreusableview *headerview = [CollectionView dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "header" Forindexpath:indexpath];

Headerview.backgroundcolor = [Uicolor Whitecolor];

return headerview;

} else {

Tail area

Uicollectionreusableview *footerview = [CollectionView dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionfooter withreuseidentifier:@ "Footer" Forindexpath:indexpath];

Footerview.backgroundcolor = [Uicolor Redcolor];

return footerview;

}

}

#pragma Mark's handling of item clicks

-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Section:%ld row:%ld", Indexpath.section, Indexpath.row);

}

-(void) addallviews

{

Initializing the collection View

1. Creating a Layout object

Uicollectionviewflowlayout *flowlayout = [Uicollectionviewflowlayout new];

Property

Set the size of each one

Flowlayout.itemsize = Cgsizemake (110, 100);

Set the minimum spacing between left and right

flowlayout.minimuminteritemspacing = 5;

Set minimum spacing up and down

flowlayout.minimumlinespacing = 5;

Scrolling direction

Flowlayout.scrolldirection = uicollectionviewscrolldirectionvertical;

Set the size of the header area

Flowlayout.headerreferencesize = Cgsizemake (Self.bounds.size.width, 20);

Set the size of the tail area

Flowlayout.footerreferencesize = Cgsizemake (Self.bounds.size.width, 20);

Set the value of the edge embedding

Flowlayout.sectioninset = Uiedgeinsetsmake (5, 10, 5, 10);

2. Use layouts to create collections views,

Self.collectionview = [[[Uicollectionview alloc] InitWithFrame:self.bounds Collectionviewlayout:flowlayout] Autorelease];

[FlowLayout release];

[Self addsubview:_collectionview];

}

-(Instancetype) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self) {

[Self addallviews];

Customizing the collection View cell setting rounded corners

Self.layer.masksToBounds = YES;

Self.layer.cornerRadius = 15;

}

return self;

}

-(void) addallviews

{

Self.label = [[[UILabel Alloc] Initwithframe:cgrectmake (5, 5, +)] [autorelease];

_label.backgroundcolor = [Uicolor Yellowcolor];

_label.layer.maskstobounds = YES;

_label.layer.cornerradius = 10;

[Self.contentview Addsubview:_label];

}

Collection View 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.