1. UIcollectionView是UItableView的加强版
UItablevie和UIcollectionView的设计思想
1.布局:
UItableView布局可以有UItableView本身和UItableViewDelegate
UIcollectionView的布局有UIcollectionViewlayout的子类UIcollectionflowlayoutDelegate完成
2.布局样式
UItableView单列多行
UIcollectionView支持多行多列
3.数据源
UItableView的数据源是UItableViewDataSource
UIcollectionView的数据源是UIcollectionViewDataSource
4.cell的样式
UItableViewCell系统提供的有四种样式
UIcollectionViewCell只带contentview,但是contentview什么都没有,所有你要显示图片,文字必须自定义Cell
5.Cell的重用
UItableViewCell和UIcollectionViewCell都可以重用先注册后重用
6.页眉和页脚
UItableView的页眉和页脚不可以重用但是UIcollectionView的页眉和页脚是可以重用的
7.编辑
UItableView支持编辑,添加删除 移动,
UIcollectionView不支持编辑
8.父类
UItableView布局可以有UItableView本身和UItableViewDelegate
UIcollectionView的布局有UIcollectionViewlayout的子类UIcollectionflowlayoutDelegate完成
2.布局样式
UItableView单列多行
UIcollectionView支持多行多列
3.数据源
UItableView的数据源是UItableViewDataSource
UIcollectionView的数据源是UIcollectionViewDataSource
4.cell的样式
UItableViewCell系统提供的有四种样式
UIcollectionViewCell只带contentview,但是contentview什么都没有,所有你要显示图片,文字必须自定义Cell
5.Cell的重用
UItableViewCell和UIcollectionViewCell都可以重用先注册后重用
6.页眉和页脚
UItableView的页眉和页脚不可以重用但是UIcollectionView的页眉和页脚是可以重用的
7.编辑
UItableView支持编辑,添加删除 移动,
UIcollectionView不支持编辑
8.父类
UItableView和UIcollectionView父类都是UIscrollview但是UItableView只能上下滚动,而UIcollectionView支持上下方向和左右方向滚动
2.
- (void)configureCollectionView
{
// 创建一个UIcollectionView对象
// UICollectionViewLayout是所有布局类的基类,是一个抽象的类,一般很少直接使用基类,都是使用基类的子类,所有collectionView布局要使用UIcollectionViewflowlayout不是视图
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 设置item的大小
flowLayout.itemSize = CGSizeMake(130, 150);
// 设置item的缩减量
flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
// 设置最小行间距
flowLayout.minimumLineSpacing = 20.0;
// 设置item之间的列间距
flowLayout.minimumInteritemSpacing = 20.0;
// 设置collectionView滚动的方法
// flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 设置页眉的大小
flowLayout.headerReferenceSize = CGSizeMake(0, 40);
// 设置页脚的大小
flowLayout.footerReferenceSize = CGSizeMake(0, 20);
UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
// 配置collectionView的颜色
collectView.backgroundColor = [UIColor brownColor];
// 指定数据源代理
collectView.dataSource = self;
// 设置业务代理
collectView.delegate = self;
// 注册Cell
[collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kItem];
// 注册页眉和页脚
// 页眉
// 第一个参数:重用视图的类 第二个参数:重用的是页眉还是页脚的种类 第三个参数:重用的标示
[collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader];
// 页脚
[collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter];
// 将cllectonView添加到视图控制器上
[self.view addSubview:collectView];
[collectView release];
{
// 创建一个UIcollectionView对象
// UICollectionViewLayout是所有布局类的基类,是一个抽象的类,一般很少直接使用基类,都是使用基类的子类,所有collectionView布局要使用UIcollectionViewflowlayout不是视图
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 设置item的大小
flowLayout.itemSize = CGSizeMake(130, 150);
// 设置item的缩减量
flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
// 设置最小行间距
flowLayout.minimumLineSpacing = 20.0;
// 设置item之间的列间距
flowLayout.minimumInteritemSpacing = 20.0;
// 设置collectionView滚动的方法
// flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 设置页眉的大小
flowLayout.headerReferenceSize = CGSizeMake(0, 40);
// 设置页脚的大小
flowLayout.footerReferenceSize = CGSizeMake(0, 20);
UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
// 配置collectionView的颜色
collectView.backgroundColor = [UIColor brownColor];
// 指定数据源代理
collectView.dataSource = self;
// 设置业务代理
collectView.delegate = self;
// 注册Cell
[collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kItem];
// 注册页眉和页脚
// 页眉
// 第一个参数:重用视图的类 第二个参数:重用的是页眉还是页脚的种类 第三个参数:重用的标示
[collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader];
// 页脚
[collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter];
// 将cllectonView添加到视图控制器上
[self.view addSubview:collectView];
[collectView release];
[flowLayout release];
}
3. #pragma mark数据源代理
//返回每个分区的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
//根据indexpath返回Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kItem forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];//设置Cell的颜色
NSLog(@"%@",NSStringFromCGRect(cell.frame));
return cell;
}
//返回collectionView的分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//返回重用的页眉页脚的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view = nil;
// 根据种类判断要重用的页眉还是页脚
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 重用页眉
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader forIndexPath:indexPath];
view.backgroundColor = [UIColor whiteColor];//设置页眉视图的背景颜色
}else{
// 重用页脚
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
//返回每个分区的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
//根据indexpath返回Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kItem forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];//设置Cell的颜色
NSLog(@"%@",NSStringFromCGRect(cell.frame));
return cell;
}
//返回collectionView的分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//返回重用的页眉页脚的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view = nil;
// 根据种类判断要重用的页眉还是页脚
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 重用页眉
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeader forIndexPath:indexPath];
view.backgroundColor = [UIColor whiteColor];//设置页眉视图的背景颜色
}else{
// 重用页脚
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
view.backgroundColor = [UIColor grayColor];
}
return view;
}
#pragma mark collectionView的业务代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// 打印item的分区下标和item的下标
NSLog(@"%ld %ld",indexPath.row,indexPath.section);
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// 打印item的分区下标和item的下标
NSLog(@"%ld %ld",indexPath.row,indexPath.section);
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
#pragma mark UICollectionViewLayoutDelegate方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 返回每个item大小
if (0 == indexPath.section) {
return CGSizeMake(50, 50);
}else{
return CGSizeMake(130, 100);
}
}
//返回分区的缩进量
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if (0==section%2) {
return UIEdgeInsetsMake(10, 10, 10, 10);
}else
{
return UIEdgeInsetsMake(20, 20, 20, 20);
}
}
//返回每一行item之间直接的最新间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 30;
}
//返回item之间的列间距最小的
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 20;
}
//返回页眉的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(320, 100);
}
//返回页脚的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(320, 50);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 返回每个item大小
if (0 == indexPath.section) {
return CGSizeMake(50, 50);
}else{
return CGSizeMake(130, 100);
}
}
//返回分区的缩进量
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if (0==section%2) {
return UIEdgeInsetsMake(10, 10, 10, 10);
}else
{
return UIEdgeInsetsMake(20, 20, 20, 20);
}
}
//返回每一行item之间直接的最新间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 30;
}
//返回item之间的列间距最小的
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 20;
}
//返回页眉的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(320, 100);
}
//返回页脚的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(320, 50);
}