在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. python三大神器之fabric(2.0新特性)

    fabric经常出现在自动化运维领域,批量处理一些运维工作.fabric是在paramiko之上又封装了一层,操作起来更加简单易用. 本来只是想写个博客记录一下,然后发现之前写的代码不能运行了,报以下 ...

  2. vue-router 基本使用

    参考原文:http://www.cnblogs.com/SamWeb/p/6610733.html 此链接讲了大部分常用的路由配置及使用,下星期总结

  3. SpringBoot(10) Servlet3.0的注解:自定义原生Servlet、自定义原生Listener

    一.自定义原生Servlet 1.启动类里面增加注解 @ServletComponentScan 2.Servlet上添加注解  @WebServlet(name = "userServle ...

  4. .net 数据类型转换int.Parse()与int.TryParse

    int.Parse()是一种类容转换:表示将数字内容的字符串转为int类型. 如果字符串为空,则抛出ArgumentNullException异常: 如果字符串内容不是数字,则抛出FormatExce ...

  5. MVC查找排序分页学习

    2018-08-07 16:04:11 实现常用功能(Index.cshtml中) 1.查找 2.用户名排序(点击用户名) 3.分页功能(数据库MVCDemo可以添加用户) 源代码参考如下: 链接:  ...

  6. 浅谈Token理解运用

    周末没带电脑,闲着也是闲着,出来分享一点东西,也当自己学习和巩固了. 今天分享一下Token的理解,首先Token的定义是什么? 概念 Token被翻译成为('令牌','标记')在计算机中的含义也差不 ...

  7. 从零开始学安全(十五)●DHCP服务

    DHCP,全名为:Dynamic Host Configuration Protocol,动态主机配置协议,它是一种基于UDP的局域网协议,其作用主要有:给主机自动分配IP地址,管理员通过该协议管理内 ...

  8. 46.Linux-创建rc红外遥控平台设备,实现重复功能(2)

    上章链接:46.Linux-分析rc红外遥控平台驱动框架,修改内核的NEC解码函数BUG(1) 在上章分析了红外platform_driver后,已经修改bug后,接下来我们自己创建一个红外platf ...

  9. [JavaScript]手机滑动图片

    思路 1.用ul ,li 来装载滑动的图片,超出部分隐藏 2.滑动是通过改变ul的位置来实现 布局 模块 1: 根据li元素个数去设置ul的宽度 1.1 获取ul元素 1.2 获取li元素的个数 1. ...

  10. 小tips:JS中typeof与instanceof用法

    介绍 typeof typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number boolean string function(函数) object(NULL, ...