在iOS开发中经常会用到UICollectionView,和UITableView同样即成UIScrollView 但是操作起来比UITableVIew要麻烦一些 ,有些地方需要注意,一下是UICollectionView基础详解。

//

//  ViewController.m

//  Collection

#import "ViewController.h"

#import "CollectionViewCell.h"

#import "CollectionViewCell1.h"

#import "CollectionReusableView.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //此处必须要有创见一个UICollectionViewFlowLayout的对象

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];

    //同一行相邻两个cell的最小间距

    layout.minimumInteritemSpacing = ;

    //最小两行之间的间距

    layout.minimumLineSpacing = ;

    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(, , , ) collectionViewLayout:layout];

    _collectionView.backgroundColor=[UIColor whiteColor];

    _collectionView.delegate=self;

    _collectionView.dataSource=self;

    //这个是横向滑动

    //layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;

    [self.view addSubview:_collectionView];

    /*

     *这是重点 必须注册cell

     */

    //这种是xib建的cell 需要这么注册

    UINib *cellNib=[UINib nibWithNibName:@"CollectionViewCell" bundle:nil];

    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CollectionViewCell"];

    //这种是自定义cell不带xib的注册

//   [_collectionView registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:@"myheheIdentifier"];

    //这种是原生cell的注册

//    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    //这是头部与脚部的注册

    UINib *cellNib1=[UINib nibWithNibName:@"CollectionReusableView" bundle:nil];

    [_collectionView registerNib:cellNib1 forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"CollectionReusableView"];

    [_collectionView registerNib:cellNib1 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionReusableView"];

}

//一共有多少个组

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return ;

}

//每一组有多少个cell

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return ;

}

//每一个cell是什么

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];

    cell.label.text=[NSString stringWithFormat:@"%ld",indexPath.section*+indexPath.row];

    cell.backgroundColor=[UIColor groupTableViewBackgroundColor];

    return cell;

}

//头部和脚部的加载

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *view=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView"forIndexPath:indexPath];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        label.text=@"头";

    }else{

        label.text=@"脚";

    }

    [view addSubview:label];

    return view;

}

//每一个分组的上左下右间距

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(, , , );

}

//头部试图的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(, );

}

//脚部试图的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{

    return CGSizeMake(, );

}

//定义每一个cell的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(, );

}

//cell的点击事件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    //cell被点击后移动的动画

    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];

}

@end

UICollectionView 基础的更多相关文章

  1. IOS UICollectionView基础+UICollectionViewFlowLayout基础

    UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...

  2. UICollectionView基础学习

    相信了解UICollectionView的也一定听过瀑布流吧,开始之前先提供两个瀑布流,有时间的可以深入研究研究 https://github.com/dingpuyu/WaterFall https ...

  3. UICollectionView基础/UICollectionViewCell的四种创建方式

    前言 UICollectionViewCell的四种创建方式:http://blog.csdn.net/ZC_Huang/article/details/52002302 这个控件,看起来与UITab ...

  4. UICollectionView基础

    初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...

  5. iOS UICollectionView基础

    转载自:http://www.cnblogs.com/wayne23/p/4013522.html  初始化部分: UICollectionViewFlowLayout *flowLayout= [[ ...

  6. UICollectionView基础API笔记

    UICollectionView系列API,属性含义笔记.在UICollectionView笔记1中我们了解了UICollectionView是什么,以及可以做什么:在UICollectionView ...

  7. UICollectionView基础用法

    初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...

  8. UI:UICollectionView

    #import "ViewController.h" #import "HeaderView.h" #import "FooterView.h&quo ...

  9. UICollectionView——整体总结

    前言 这几天有时间看了下UICollectionView的东西,才发觉它真的非常强大,很有必要好好学习学习.以前虽然用过几次,但没有系统的整理总结过.这两天我为UICollectionView做一个比 ...

随机推荐

  1. 项目详解2—LVS负载均衡详解

    一.负载均衡集群介绍 1.集群 ① 集群(cluster)技术是一种较新的技术,通过集群技术,可以在付出较低成本的情况下获得在性能.可靠性.灵活性方面的相对较高的收益,其任务调度则是集群系统中的核心技 ...

  2. Mysql数据库的二进制安装和基础入门操作

    前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...

  3. 又是一个Notepad++的问题

    昨天刚刚解决了Style Configurator无法保存的问题,今天继续解决另一个. 装好Notepad++之后,可以在右键菜单里面看到 Edit With Notepad++,win7里面挺好用的 ...

  4. Winform系列——好用的DataGridview过滤控件(表格的高级搜索功能)

    上一篇 Winform系列——好看的DataGridView折叠控件 中主要介绍了DataGridview的表格多级折叠功能.这章主要介绍下最近封装的另一个DataGridview表格高级过滤的功能. ...

  5. [转]如何在Angular4中引入jquery

    本文转自:https://blog.csdn.net/home_zhang/article/details/77992734 1.anjq是我的项目名称: 在anjq目录下打开dos命令窗口,然后依次 ...

  6. 第一册:lesson eighty nine.

    原文: For sale. A:Good afternoon. I believe that the house is for sale. B:That's right. A:May I have a ...

  7. EF 传递的主键值的数量必须与实体上定义的主键值的数量匹配 原因

    主要是该数据表没有定义主键造成的

  8. [android] 显示意图激活另外一个activity

    可以使用跳转的方式类似javaweb来实现界面转换 显示意图就是必须要指定开启组件的具体信息,包名,组件名,组件的class 新建一个类TwoActivity ,继承Activity类,重写onCre ...

  9. python基础学习(二)注释和算术运算符

    注释 1. 注释的作用 注释就是对某些代码进行标注说明,以增强代码的可读性.我们在写程序的时候,编写的某一部分代码的意图不太明显,这时候就需要对这一部分代码加以说明,来明确这一部分到的意图.一般的编程 ...

  10. regression and anova

    regression一般是统计学的回归回归,研究一个随机变量Y对另一个(X)或一组(X1,X2,-,Xk)变量的相依关系的统计分析方法.研究一 个或多个随机变量Y1 ,Y2 ,-,Yi与另一些变量X1 ...