UICollectionView的简单使用

一.常用方法和属性变量

1.创建方式

UICollectionView *collectionView = [[UICollectionView alloc] 
          initWithFrame:self.view.bounds 
                     collectionViewLayout:flowLayout];

2.创建的时候需要一个布局方式

流水式布局-用于管理每个item显示的样式

UICollectionViewFlowLayout *flowLayout = 
                 [[UICollectionViewFlowLayout alloc] init];

每个item的大小

flowLayout.itemSize = CGSizeMake(100, 100);

item行与行之间的最小间距

flowLayout.minimumLineSpacing = 10;

item列与列之间的最小间距

flowLayout.minimumInteritemSpacing = 10;

每个section之间的间距

flowLayout.sectionInset = UIEdgeInsetsMake(0, 50, 0, 0);

设置滚动方向

flowLayout.scrollDirection = 
         UICollectionViewScrollDirectionHorizontal;

3.设置代理

collectionView.delegate = self;
collectionView.dataSource = self;

4.注册

[collectionView registerClass:[UICollectionViewCell class] 
                        forCellWithReuseIdentifier:@"cellID"];

5.配置有多少个section

-(NSInteger)numberOfSectionsInCollectionView:
                          (UICollectionView *)collectionView{
    return 3;
}

6.配置每个section有多少个item

-(NSInteger)collectionView:(UICollectionView *)collectionView 
                    numberOfItemsInSection:(NSInteger)section{
    return 9;
}

7.配置每个item显示什么

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
    //背景颜色
    if (indexPath.section == 0) {
        cell.backgroundColor = [UIColor redColor];
    }else if(indexPath.section == 1){
        cell.backgroundColor = [UIColor orangeColor];
    }else{
        cell.backgroundColor = [UIColor greenColor];
    }
    
    //返回
    return cell;
}

二.简单的使用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //流水式布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //用于管理每个item显示的样式
    flowLayout.itemSize = CGSizeMake(100, 100);
    
    //item行与行之间的最小间距
    flowLayout.minimumLineSpacing = 10;
    
    //item列与列之间的最小间距
    flowLayout.minimumInteritemSpacing = 10;
    
    //每个section之间的间距
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 50, 0, 0);
    
    //设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //创建collectionView 集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    //设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    //显示
    [self.view addSubview:collectionView];
    
    //注册cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
}

//配置有多少个section
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

//配置每个section有多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}

//配置每个item显示什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
    //背景颜色
    if (indexPath.section == 0) {
        cell.backgroundColor = [UIColor redColor];
    }else if(indexPath.section == 1){
        cell.backgroundColor = [UIColor orangeColor];
    }else{
        cell.backgroundColor = [UIColor greenColor];
    }
    
    //返回
    return cell;
}
@end

三.运行结果

UICollectionView的简单使用_第1张图片
运行结果

你可能感兴趣的:(UICollectionView的简单使用)