你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios UICollectionView的使用

ios UICollectionView的使用

編輯:IOS開發綜合

UICollectionView的使用有兩種方法,一種是繼承UICollectionViewController,這個Controller會自帶一個UICollectionView;另外一種是作為一個視圖放在普通的UIViewController裡面。

個人更喜歡第二種。下面采用第二種方式簡單介紹一下UICollectionView的使用。

1.UIViewController實現委托,代碼如下

@interface YourViewController : UIViewController

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
#import
@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


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved