码迷,mamicode.com
首页 > 其他好文 > 详细

CollectionView 简用

时间:2016-02-19 20:29:32      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

 

创建一个CollectionView 分为几个步骤

1.先创建布局FlowLayout 设置布局格式

2.创建CollectionView 并使用布局Flowlayout  -initWithFrame: collectionViewLayout:

3.遵循协议delegate dataSource

4.注册cell

5.添加到view

 

#import "ViewController.h"
#import "reusableview.h"

 

- (void)viewDidLoad {
    [super viewDidLoad];
   //创建布局
    UICollectionViewFlowLayout * FL = [[UICollectionViewFlowLayout alloc]init];

  //设置布局格式
    [FL setScrollDirection:UICollectionViewScrollDirectionVertical];

  // 设置分组头大小
    FL.headerReferenceSize = CGSizeMake(100, 50);
  //创建collectionview 并且 遵循FL格式
    UICollectionView * collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:FL ];
    
    collect.delegate = self;
    collect.dataSource = self;
   //注册uicollectionviewcell
    [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

  //注册    重写uicollectionreusableView  复用类,只复用类中内容
    [collect registerClass:[reusableview class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    
    [self.view addSubview:collect];
}
#pragma make - dataSource
//cell 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}
//设置分组个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
//cell 设定
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusable = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        
        reusableview *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        view.title.text = [[NSString alloc] initWithFormat:@"头部视图%ld",indexPath.section];
        reusable = view;
    }
    return reusable;
}
#pragma make - FlowLayout
//定义cell 的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(96, 100);
}
//cell上下间距的大小
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 25;
}
//定义每个uicollectionview 的marger
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake((HEIGHT-100*3-50)/2, (WIDTH-96*2-30)/2, (HEIGHT-100*3-50)/2, (WIDTH-96*2-30)/2);
}

CollectionView 简用

标签:

原文地址:http://www.cnblogs.com/GGBigBong/p/5202033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!