ios UICollectionView的使用
2013-10-21 16:31
274 查看
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
个人更喜欢第二种。下面采用第二种方式简单介绍一下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
相关文章推荐
- ios UICollectionView的使用
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
- iOS UICollectionView简单使用
- iOS中UICollectionView(集合视图)的使用
- IOS UICollectionView 的基础使用
- iOS - UICollectionView的使用
- 李洪强iOS开发之UICollectionView的使用
- iOS开发之UICollectionViewController系列(三) :使用UICollectionView自定义瀑布流
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
- iOS 使用MJRefrsh上拉加载,下拉刷新cell不充满UICollectionView时不能刷新的问题
- iOS UICollectionViewController基本使用方法和简单的相册实现
- iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新
- iOS UICollectionView的使用(用storyboard和xib创建)
- 使用iOS控件UICollectionView生成可拖动的桌面
- [IOS]UICollectionView的使用
- IOS中UICollectionView中Decoration View的使用
- iOS开发之UICollectionView使用
- iOS UICollectionView 入门 04 使用UICollectionView的准备工作
- iOS开发——高级篇——流水布局UICollectionViewFlowLayout的基本使用
- iOS 6 开发----应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格