UI day 19 UICollectionView

1.   UIcollectionViewUItableView的加强版
    UItablevieUIcollectionView的设计思想
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.
父类
 UItableViewUIcollectionView父类都是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];
    [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];
        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];
    [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);
}










































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值