UI day 19 UICollectionView

来源:互联网 发布:feynman 知乎 编辑:程序博客网 时间:2024/06/06 16:31
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 = [[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];
        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];
    [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
原创粉丝点击