标签:
创建:
/*
UICollectionViewFlowLayout
是系统提供的UICollectionViewLayout
用于直接进行简单的横向排版、竖直排版
*/
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];
/*
scrollDirection滚动方向
UICollectionViewScrollDirectionHorizontal:横
UICollectionViewScrollDirectionVertical:竖
*/
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
/*
UICollectionView
初始化必须给个UICollectionViewLayout
其使用方法特别类似UITableView
也有俩个代理delegate、dataSource
*/
UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:self.view.boundscollectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
/*
collectionview使用注册特别的简单
注册之后根本不需要考虑为空怎么办
注册时系统自动判断是否有空余的没有就创建
其中注册时可以使用xib或者纯代码
以下分别注册单元格、头、尾
其中注册头、尾时使用的一个方法所有需要声明你注册的哪个
UICollectionElementKindSectionHeader:头
UICollectionElementKindSectionFooter:尾
*/
[collectionView registerClass:[CollectionViewCellclass] forCellWithReuseIdentifier:@"cell"];
[collectionView registerClass:[ReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];
[collectionView registerClass:[ReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"];
[self.viewaddSubview:collectionView];
#pragma mark - UICollectionViewDataSource
//返回几组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 5;
}
//返回几个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return21;
}
//返回单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
/*
注册,注册之前,前面必须注册过
注册时直接输入IndexPath就有了
我这边直接自定义单元格
*/
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
switch (indexPath.section) {
case0:
cell.backgroundColor = [UIColorredColor];
break;
case1:
cell.backgroundColor = [UIColoryellowColor];
break;
case2:
cell.backgroundColor = [UIColorblueColor];
break;
case3:
cell.backgroundColor = [UIColorpurpleColor];
break;
case4:
cell.backgroundColor = [UIColorgreenColor];
break;
default:
break;
}
cell.titleLabel.text = [NSStringstringWithFormat:@"%d -- %d",indexPath.section,indexPath.row];
return cell;
}
//返回头尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//声明头尾
ReusableView *reusable = nil;
//判断当前需要头还是尾
if (kind == UICollectionElementKindSectionHeader) {
//注册头
reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"forIndexPath:indexPath];
reusable.backgroundColor = [UIColorwhiteColor];
reusable.titleLabel.text = [NSStringstringWithFormat: @"头:%d",indexPath.section];
}else{
//注册尾
reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"forIndexPath:indexPath];
reusable.backgroundColor = [UIColorgrayColor];
reusable.titleLabel.text = [NSStringstringWithFormat: @"尾:%d",indexPath.section];
}
return reusable;
}
#pragma mark - UICollectionViewDelegate
#pragma mark - UICollectionViewDelegateFlowLayout
//返回单元格宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
float width = 150.0f;
float height = 100.0f;
returnCGSizeMake(width, height);
}
//返回单元格上左下右
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets edge = UIEdgeInsetsMake(10, 5, 10, 5);
return edge;
}
//返回头宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
float width = 150.0f;
float height = 100.0f;
returnCGSizeMake(width, height);
}
//返回尾宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
float width = 100.0f;
float height = 100.0f;
returnCGSizeMake(width, height);
}
标签:
原文地址:http://www.cnblogs.com/Zayne/p/5028567.html