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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。