一.UICollectionViewDataSource

1.返回Section数量的方法

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return ;
}

2.返回每个Section中Cell的数量的方法

- (NSInteger)collectionView: (UICollectionView *)collectionView
numberOfItemsInSection: (NSInteger)section {
return ;
}

3.选择CollectionView中所使用的Cell

- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView
cellForItemAtIndexPath: (NSIndexPath *)indexPath { //通过Cell重用标示符来获取Cell
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: reuseIdentifier
forIndexPath: indexPath];
return cell;
}

4.注册UICollectionReusableView的方法。

UINib *headerNib = [UINib nibWithNibName: @"CollectionHeaderReusableView"
bundle: [NSBundle mainBundle]];
//注册重用View
[self.collectionView registerNib: headerNib
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
withReuseIdentifier: @"CollectionHeaderReusableView"]; //注册FooterView
UINib *footerNib = [UINib nibWithNibName: @"CollectionFooterReusableView"
bundle:[ NSBundle mainBundle]]; [self.collectionView registerNib: footerNib
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter
withReuseIdentifier: @"CollectionFooterReusableView"];

5.在UICollectionViewDataSource中的设置Supplementary View

- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView
viewForSupplementaryElementOfKind: (NSString *)kind
atIndexPath: (NSIndexPath *)indexPath{
//设置SectionHeader
if ([kind isEqualToString: UICollectionElementKindSectionHeader]) { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderReusableView" forIndexPath:indexPath]; return view;
} //设置SectionFooter
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFooterReusableView" forIndexPath:indexPath];
return view; }

二.UICollectionViewDelegateFlowLayout

1.Cell定制尺寸

- (CGSize)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath: (NSIndexPath *)indexPath{ if (indexPath.section == ) {
return CGSizeMake(, );
} return CGSizeMake(, );
}

2.改变Section的上下左右边距--UIEdgeInsetsMake

- (UIEdgeInsets)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex: (NSInteger)section{ if (section == ) {
return UIEdgeInsetsMake(, , , );
}
return UIEdgeInsetsMake(, , , );
}

3.每个Cell的上下边距

- (CGFloat)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex: (NSInteger)section{
if (section == ) {
return 5.0f;
}
return 20.0f;
}

4.设置Cell的左右边距

- (CGFloat)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex: (NSInteger)section{
if (section == ) {
return 5.0f;
}
return 20.0f;
}

5.设置Header View和Footer View的大小

- (CGSize)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection: (NSInteger)section{
return CGSizeMake(, );
} - (CGSize)collectionView: (UICollectionView *)collectionView
layout: (UICollectionViewLayout*)collectionViewLayout
referenceSizeForFooterInSection: (NSInteger)section{
return CGSizeMake(, );
}

三.UICollectionViewDelegate

1.设置Cell可以高亮

- (BOOL)collectionView: (UICollectionView *)collectionView
shouldHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ return YES; }

2.Cell从非高亮变为高亮状态和从高亮变为非高亮状态时回调用下面的方法

- (void)collectionView: (UICollectionView *)collectionView
didHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ [self changeHighlightCellWithIndexPath:indexPath];
} - (void)collectionView: (UICollectionView *)collectionView
didUnhighlightItemAtIndexPath: (NSIndexPath *)indexPath{ [self changeHighlightCellWithIndexPath:indexPath]; }

3.设定Cell是否可选

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}

4.Cell支持多选

 self.collectionView.allowsMultipleSelection = YES;

5.在多选状态下需要支持取消Cell的多选

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}

6.Cell将要出现,Cell出现后,Supplementary View将要出现以及Supplementary View已经出现

/**
* Cell将要出现的时候调用该方法
*/
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0){
NSLog(@"第%ld个Section上第%ld个Cell将要出现",indexPath.section ,indexPath.row);
} /**
* Cell出现后调用该方法
*/
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"第%ld个Section上第%ld个Cell已经出现",indexPath.section ,indexPath.row);
} /**
* headerView或者footerView将要出现的时候调用该方法
*/
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0){ NSLog(@"第%ld个Section上第%ld个扩展View将要出现",indexPath.section ,indexPath.row); } /**
* headerView或者footerView出现后调用该方法
*/
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{ NSLog(@"第%ld个Section上第%ld个扩展View已经出现",indexPath.section ,indexPath.row); }

iOS-UICollectionViewController协议及回调的更多相关文章

  1. 调用Live555接收RTSP直播流,转换为Http Live Streaming(iOS直播)协议

    Live555接收RTSP直播流,转换Http Live Streaming(iOS直播)协议 RTSP协议也是广泛使用的直播/点播流媒体协议,之前实现过一个通过live555接收RTSP协议,然后转 ...

  2. fir.im Weekly - 揭秘 iOS 面向协议编程

    本期 fir.im Weekly 重点推荐关于 iOS 面向协议编程相关文章,还有 iOS 多线程安全.Swift 进阶.Android MVVM 应用框架.Android 蓝牙实践等技术文章分享和工 ...

  3. IOS Objective-C 协议,委托

    IOS Objective-C 协议,委托 IOS开发使用的语言Objective-C(以下简称OBJ-C)是一种扩展自C语言的面向对象语言.在OBJ-C中有一个很重要概念:消息.在最近的学习当中逐渐 ...

  4. iOS xmpp协议实现聊天之openfire的服务端配置(一)

    今天弄这个openfire服务端的配置直接苦了一逼,只是好在最后最终配置好了.首先感谢@月光的尽头的博客给了我莫大的帮助. 切入正题,首先说一下iOS xmpp协议实现聊天openfireserver ...

  5. Objective-C学习笔记 利用协议实现回调函数

    来源:http://mobile.51cto.com/iphone-278354.htm Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然 ...

  6. iOS学习笔记之回调(一)

    什么是回调 看了好多关于回调的解释的资料,一开始总觉得这个概念理解起来有点困难,可能是因为自己很少遇到这种类型的调用吧.探索良久之后,才算有点启发,下面是自己的一点理解. 我们知道,在OSI网络七层模 ...

  7. IOS http协议 总结

    HTTP协议1.面试题常见:聊一下HTTP协议(协议的完整的通信过程) ============================================================ 一.一 ...

  8. iOS网络协议 HTTP/TCP/IP浅析

    一.TCP/IP协议       话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...

  9. iOS - UICollectionViewController

    前言 NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView @available(iOS 6.0, *) pub ...

  10. iOS学习笔记之回调(二)

    写在前面 上一篇学习笔记中简单介绍了通过目标-动作对实现回调操作:创建两个对象timer和logger,将logger设置为timer的目标,timer定时调用logger的sayOuch函数.在这个 ...

随机推荐

  1. Jlink-10 pin 的定义(stm32使用)官方定义

    因为在网上找了好久才找到正确的接法,所以专门记载了下来,因为stm32芯片这几个功能引脚会内置上拉电阻,所以不需要再外接电阻了.

  2. vs code 写C#心得

    !!!官方指南请访问: https://docs.microsoft.com/zh-cn/dotnet/core/index code runner 插件 ---------------------- ...

  3. Java线程等待与唤醒

    class ThreadA extends Thread{ public ThreadA(String name) { super(name); } public void run() { synch ...

  4. 【8086汇编-Day1】预备知识

    菜鸟的8086汇编入门之旅,偶有错处恭请大佬们指正. Ⅰ· 闲说一下 我为什么学汇编?相对于晦涩难懂的01010101011010机器语言(高低电平变化驱动机器做出不同反应),汇编语言用更便于记忆和使 ...

  5. 课上实践练习——MyOd

    编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 Linux下od -tx -tc XXX的功能 1.功能 od命令用于将指定文件内容以八进制.十 ...

  6. 2017-2018-1 20155330《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155330<信息安全技术>实验二--Windows口令破解 姓名:朱玥 学号:20155330 班级:201553 日期:2017.10.24 实验环境 系统 ...

  7. Web前端优化常用规则

    一,尽量减少HTTP请求 二,使用CDN(内容分发网络) 三,添加Expire/Cache-Control头 四,启用Gzip压缩 五,将CSS放在层叠样式表放到head里面 六,将Script放到页 ...

  8. 使用salt-ssh初始化系统安装salt-minion

    salt-ssh介绍及使用方法 在ssh上执行salt命令和状态而不安装salt-minion,类似于ansible. 1. salt-ssh的安装: [root@linux-node1 ~]# yu ...

  9. javaweb(三十八)——mysql事务和锁InnoDB(扩展)

    MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备就My ...

  10. PHP导出Excel,设置表格样式,填充颜色等较为复杂样式

    // 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档 <?php //引入Li类对数据进行操作include_once('./Li.php');//引入Excel类库对对数据进行操作i ...