ios UICollectionView的使用

时间:2025-01-30 08:00:35

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

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

实现委托,代码如下

@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: collectionViewLayout:layout];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    =[UIColor whiteColor];
    =self;
    =self;
    [ 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 ) {
        if (view) {
            [view removeFromSuperview];
        }
    }
    UIImageView*imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ITEM_WIDTH, ITEM_WIDTH)];
    if (>4) {
        =[UIImage imageNamed:@""];
    }else{
        =[UIImage imageNamed:@""];
    }
    [ addSubview:imgView];
    [imgView release];
    return  cell;
}

的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(130,130);
}


CircleLayout的代码:

#import <UIKit/>
#import <QuartzCore/>
@interface CircleLayout : UICollectionViewLayout
@property (nonatomic, assign) CGPoint center;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) NSInteger cellCount;
@end



#define ITEM_SIZE 130
#import ""

@implementation CircleLayout
@synthesize cellCount,center,radius;
- (void)prepareLayout{
    [super prepareLayout];
    CGSize size = ;
    cellCount=[ numberOfItemsInSection:0];
    center=CGPointMake(/2, /2);
    radius = MIN(, ) / 2.5;
}
-(CGSize)collectionViewContentSize{
    return [self collectionView].;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    = CGPointMake( + radius * cosf(2 * * M_PI /cellCount),
                                    
                                    + radius * sinf(2 * * M_PI /cellCount));
    return attributes;
}
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray* attributes = [NSMutableArray array];
    for (NSInteger i=0 ; i < ; i++) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath *)itemIndexPath{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    = 0.0;
    = CGPointMake(, );
    return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    = 0.0;
    = CGPointMake(, );
    attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
    return attributes;
}
@end