ios UICollectionView的使用

UICollectionView的使用有两种方法,一种是继承UICollectionViewController,这个Controller会自带一个UICollectionView;另外一种是作为一个视图放在普通的UIViewController里面。

个人更喜欢第二种。下面采用第二种方式简单介绍一下UICollectionView的使用。

1.UIViewController实现委托,代码如下

@interface YourViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

2.声明UICollectionView,代码如下

@property(nonatomic,retain)UICollectionView*myCollectionView;

3.初始化UICollectionView,代码如下

- (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];

}

这里面的CircleLayout继承自UICollectionViewLayout,主要用来表现UICollectionView的布局以及一些属性。

4.实现- (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;

}

5.cell的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

return CGSizeMake(130,130);

}

CircleLayout的代码:

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

时间: 2024-08-28 15:44:10

ios UICollectionView的使用的相关文章

iOS UICollectionView与UITableView

共同点:都需要接受两个协议 并执行代理方法 不同点:初始化方法不同  UITableVIew可以用alloc 方法初始化 而UICollectionView必须用下面方法初始化 // 初始化瀑布流 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setItemSize:CGSizeMake(150,120)]; //设置每个cell显示数据的宽和高必须

iOS UICollectionView 使用详解

// 代码创建 所属controller要遵循三个协议:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; [flowLayout setItemSize:CGSizeMake(70, 100)];//设置cel

iOS UICollectionView之-(水平滚动)

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end

iOS UIcollectionView 实现卡牌翻转效果

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"卡牌翻转效果"; self.edgesForExtendedLayout = UIRectEdgeNone; [self.view addSubview:self.collectionView]; NSArray * arr =

iOS UICollectionView 实现轮播图

利用UICollectionView 实现轮播图 : 具体代码如下, 简单粗暴, : <span style="font-size:24px;">// // ViewController.m // CollectionPhotosView // // Created by 帝炎魔 on 16/5/30. // Copyright © 2016年 帝炎魔. All rights reserved. // /** * UICollectionView 实现轮播图的实现 将定时器

iOS UICollectionView简单使用

首先认识一下UICollectionView NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类. 使用UICollectionView 必须实现UICo

iOS:UICollectionView纯自定义的布局:瀑布流布局

创建瀑布流有三种方式: 第一种方式:在一个ScrollView里面放入三个单元格高度一样的tableView,禁止tableView滚动,只需让tableView随着ScrollView滚动即可.这种方式太奇葩,不太推荐使用... 第二种方式:在一个ScrollView里面从左到右依次放入三个UIView,当ScrollView滚动时,如果之前的三个view消失就将它们存入自定义的缓冲池,即数组中,下拉时再从数组中取出这三个view放到之前三个view位置的下面.但是,切记,每次要依次计算补全最

ios UICollectionView简单说明

原谅我记不住写下来好了 UICollectionViewFlowLayout 流式自动布局 继承于UICollectionViewLayout 初始化:[[UICollectionViewFlowLayout alloc]init] 属性说明: minimumLineSpacing:cell上下间距 minimumInteritemSpacing:左右间距 itemSize:cell的size scrollDirection:方向 sectionInset:模块的边距 headerReferen

iOS UICollectionView之三(基本用法)

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end

iOS UICollectionView的使用(用storyboard和xib创建)

1. 在storyboard中,拖出1个UICollectionViewController 2. 新建file--Cocoa Touch Class,继承自UICollectionViewController,假设名字是CollectionDemo 3. 在storyboard, 把刚才拖出来的UICollectionViewController的class改成CollectionDemo 4. 在 CollectionDemo.m 中实现,数据源方法 - (NSInteger)collect