Use of iOS Uicollectionview

Source: Internet
Author: User

There are two ways to use Uicollectionview, one is to inherit Uicollectionviewcontroller, and this controller will bring a Uicollectionview The other is to be placed in a normal uiviewcontroller as a view.

Individuals prefer the second kind. Here is a second way to briefly introduce the use of Uicollectionview.

1.UIViewController implementation of the delegate, the code is as follows

@interface yourviewcontroller:uiviewcontroller<uicollectionviewdatasource,uicollectionviewdelegate>

2. Declare Uicollectionview, the code is as follows

@property (Nonatomic,retain) Uicollectionview*mycollectionview;

3. Initialize the Uicollectionview, the code is as follows

-(void) viewdidload
{
    [Super Viewdidload];
    [self Initconllectionview];
}
-(void) initconllectionview{
    circlelayout*layout=[[circlelayout alloc] init];
    Mycollectionview=[[uicollectionview alloc] initWithFrame:self.view.bounds collectionviewlayout: Layout];
    [Mycollectionview Registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@ " Cell "];
    Mycollectionview.backgroundcolor=[uicolor Whitecolor];
    mycollectionview.delegate=self;
    mycollectionview.datasource=self;
    [Self.view Addsubview:mycollectionview];
    [Layout release];
}

This circlelayout inherits from Uicollectionviewlayout, and is mainly used to show the layout of Uicollectionview and some properties.

4. Implementation-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) Indexpath;

-(uicollectionviewcell*) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{
Uicollectionviewcell*cell=[collectionview Dequeuereusablecellwithreuseidentifier:cell_str ForIndexPath:indexPath ];
For (Uiview*view in cell.contentView.subviews) {
if (view) {
[View Removefromsuperview];
}
}
Uiimageview*imgview=[[uiimageview Alloc] Initwithframe:cgrectmake (0, 0, Item_width, item_width)];
if (indexpath.row>4) {
Imgview.image=[uiimage imagenamed:@ "Apple.png"];
}else{
Imgview.image=[uiimage imagenamed:@ "Applec.png"];
}
[Cell.contentview Addsubview:imgview];
[Imgview release];
return cell;
}

Size of 5.cell

-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{
Return Cgsizemake (130,130);
}


Circlelayout's Code:

CircleLayout.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface Circlelayout:uicollectionviewlayout
@property (nonatomic, assign) Cgpoint Center;
@property (nonatomic, assign) cgfloat radius;
@property (nonatomic, assign) Nsinteger Cellcount;
@end


Circlelayout.m


#define ITEM_SIZE 130
#import "CircleLayout.h"

@implementation Circlelayout
@synthesize Cellcount,center,radius;
-(void) preparelayout{
[Super Preparelayout];
Cgsize size = self.collectionView.frame.size;
Cellcount=[self.collectionview numberofitemsinsection:0];
Center=cgpointmake (SIZE.WIDTH/2, SIZE.HEIGHT/2);
Radius = MIN (size.width, size.height)/2.5;
}
-(Cgsize) collectionviewcontentsize{
return [self collectionview].frame.size;
}
-(Uicollectionviewlayoutattributes *) Layoutattributesforitematindexpath: (Nsindexpath *) path
{
uicollectionviewlayoutattributes* attributes = [uicollectionviewlayoutattributes Layoutattributesforcellwithindexpath:path];
Attributes.size = Cgsizemake (item_size, item_size);
Attributes.center = Cgpointmake (center.x + radius * COSF (2 * path.item * m_pi/cellcount),

Center.y + radius * Sinf (2 * path.item * m_pi/cellcount));
return attributes;
}
-(nsarray*) Layoutattributesforelementsinrect: (CGRect) rect{
nsmutablearray* attributes = [Nsmutablearray array];
for (Nsinteger i=0; i < Self.cellcount; i++) {
nsindexpath* Indexpath = [Nsindexpath indexpathforitem:i insection:0];
[Attributes Addobject:[self Layoutattributesforitematindexpath:indexpath];
}
return attributes;
}
-(Uicollectionviewlayoutattributes *) Initiallayoutattributesforinserteditematindexpath: (NSIndexPath *) itemindexpath{
uicollectionviewlayoutattributes* attributes = [self layoutattributesforitematindexpath:itemindexpath];
Attributes.alpha = 0.0;
Attributes.center = Cgpointmake (center.x, CENTER.Y);
return attributes;
}
-(Uicollectionviewlayoutattributes *) Finallayoutattributesfordeleteditematindexpath: (NSIndexPath *) ItemIndexPath
{
uicollectionviewlayoutattributes* attributes = [self layoutattributesforitematindexpath:itemindexpath];
Attributes.alpha = 0.0;
Attributes.center = Cgpointmake (center.x, CENTER.Y);
Attributes.transform3d = Catransform3dmakescale (0.1, 0.1, 1.0);
return attributes;
}
@end


Use of iOS Uicollectionview

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.