UI day 19 UICollectionView
来源:互联网 发布:feynman 知乎 编辑:程序博客网 时间:2024/06/06 16:31
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 = [[UICollectionViewFlowLayoutalloc]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 = [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:flowLayout];
// 配置collectionView的颜色
collectView.backgroundColor= [UIColorbrownColor];
// 指定数据源代理
collectView.dataSource= self;
// 设置业务代理
collectView.delegate= self;
// 注册Cell
[collectView registerClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:kItem];
// 注册页眉和页脚
// 页眉
// 第一个参数:重用视图的类 第二个参数:重用的是页眉还是页脚的种类 第三个参数:重用的标示
[collectView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:kHeader];
// 页脚
[collectView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:kFooter];
// 将cllectonView添加到视图控制器上
[self.viewaddSubview:collectView];
[collectView release];
{
// 创建一个UIcollectionView对象
// UICollectionViewLayout是所有布局类的基类,是一个抽象的类,一般很少直接使用基类,都是使用基类的子类,所有collectionView布局要使用UIcollectionViewflowlayout不是视图
UICollectionViewFlowLayout*flowLayout = [[UICollectionViewFlowLayoutalloc]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 = [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:flowLayout];
// 配置collectionView的颜色
collectView.backgroundColor= [UIColorbrownColor];
// 指定数据源代理
collectView.dataSource= self;
// 设置业务代理
collectView.delegate= self;
// 注册Cell
[collectView registerClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:kItem];
// 注册页眉和页脚
// 页眉
// 第一个参数:重用视图的类 第二个参数:重用的是页眉还是页脚的种类 第三个参数:重用的标示
[collectView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:kHeader];
// 页脚
[collectView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:kFooter];
// 将cllectonView添加到视图控制器上
[self.viewaddSubview:collectView];
[collectView release];
[flowLayoutrelease];
}
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:kItemforIndexPath:indexPath];
cell.backgroundColor= [UIColorgreenColor];//设置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:UICollectionElementKindSectionHeaderwithReuseIdentifier:kHeaderforIndexPath:indexPath];
view.backgroundColor= [UIColorwhiteColor];//设置页眉视图的背景颜色
}else{
// 重用页脚
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:kFooterforIndexPath:indexPath];
//返回每个分区的item的个数
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
//根据indexpath返回Cell
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:kItemforIndexPath:indexPath];
cell.backgroundColor= [UIColorgreenColor];//设置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:UICollectionElementKindSectionHeaderwithReuseIdentifier:kHeaderforIndexPath:indexPath];
view.backgroundColor= [UIColorwhiteColor];//设置页眉视图的背景颜色
}else{
// 重用页脚
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:kFooterforIndexPath:indexPath];
view.backgroundColor= [UIColorgrayColor];
}
return view;
}
#pragma mark collectionView的业务代理方法
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
// 打印item的分区下标和item的下标
NSLog(@"%ld %ld",indexPath.row,indexPath.section);
DetailViewController*detailVC = [[DetailViewControlleralloc]init];
[self.navigationControllerpushViewController:detailVCanimated:YES];
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
// 打印item的分区下标和item的下标
NSLog(@"%ld %ld",indexPath.row,indexPath.section);
DetailViewController*detailVC = [[DetailViewControlleralloc]init];
[self.navigationControllerpushViewController:detailVCanimated: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);
}
0 0
- UI day 19 UICollectionView
- UI - UICollectionView
- UI 18 UICollectionView
- iOS开发UI篇 -- UICollectionView
- UI day 1 UIView
- UI day 7 UIScrollView
- UI day 9 UITabelView
- UI day 11 模态
- UI课程21 集合视图UICollectionView
- IOS开发UI篇--UICollectionView初步入门
- iOS开发-UI控件:UICollectionView基础
- iOS UI 18 uicollectionview和自定义cell
- iOS开发 UI UICollectionView 瀑布流
- iOS开发系列 ---- UI(UICollectionView的使用)
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- Android中Activity的启动模式详解
- UI day 18 数据库SQL语句
- 当expandlistview的getGroupView或者getChildView中包含checkbox时,前者点击不可用
- 线性插补法查找
- HDU1176 免费馅饼 经典DP
- UI day 19 UICollectionView
- Git@OSC代码托管(含Git教程!important非命令行)
- 选择排序复习
- UI day 20 iOS FMDB自己封装的单例类
- UI day 21 程序 进程 线程 GCD 共产党
- java并发编程学习之脏读代码示例及处理
- LeetCode 14:Longest Common Prefix
- UI day 22 block动画 CAlayer动画
- 工程优化作业——成功失败法和黄金分割法