标签:
#import "ViewController.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
//遵守协议
@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *_collectionView;
}
@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatCollectionView];
}
- (void)creatCollectionView {
//1.先创建一个 UICollectionViewFlowLayout (是集合视图的核心)
//通过UICollectionViewFlowLayout 来进行布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//2.对layout 做一些基本的设置
//设置cell 的大小(全局设置)(如果要单个对cell 大小进行设置需要遵守UICollectionViewDelegateFlowLayout协议实现方法)
layout.itemSize = CGSizeMake(100, 100);
//设置滚动的方向(水平和竖直)
//UICollectionViewScrollDirectionHorizontal水平
//UICollectionViewScrollDirectionVertical
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置 cell 之间的最小竖直间隔 (上下cell间隔)
layout.minimumLineSpacing = 30;
//cell 之间的 最小水平间隔(水平方向的cell 间隔)
layout.minimumInteritemSpacing = 10;
//设置分区头视图的大小 (如果是竖直滚动那么设置height有效,width无效,水平滚动那么width 有效)
//分区头间隔
layout.headerReferenceSize = CGSizeMake(50, 50);
//分区尾间隔(实际上就是尾视图的高)
layout.footerReferenceSize = CGSizeMake(50, 50);
//设置整个分区中所有cell(整体) 距离分区的上左下右的间距
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
//实例化一个 集合视图对象 通过layout 布局
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, kScreenSize.width, kScreenSize.height-20) collectionViewLayout:layout];
//设置代理和数据源对象
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
//cell 必须提前注册 复用标志要和下面队列获取cell 的时候一样
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
//如果设置分区的头尾视图那么需要进行注册(注册之后就可以采用复用队列)
//kind 表示 是头视图还是尾视图 第三个参数就是一个复用标识符 要和下面使用保持一致
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
//注册分区尾视图
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
[self.view addSubview:self.collectionView];
self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark UICollectionViewDataSource delegate
//有多少分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
//每个分区有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 25;
}
//获取指定分区内的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"CollectionViewCell";
//从复用队列获取 cell (需要提前注册)
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
//设置背景颜色
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
#pragma mark - 获取分区头尾视图
//创建 分区头尾视图调用
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//创建/获取头尾视图的时候也要用复用队列
//判断kind 来区分头尾
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//头视图
//(如果满足不了需要那么就定制)
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
header.backgroundColor = [UIColor greenColor];
return header;
}else {
//尾视图
//复用队列获取
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
footer.backgroundColor = [UIColor yellowColor];
return footer;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
标签:
原文地址:http://my.oschina.net/u/2410306/blog/528019