一.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. django中models的filter过滤方法

    __gt     大于__gte   大于等于 __lt      小于 __lte    小于等于 __in     存在于一个list范围内 __startswith    以...开头 __is ...

  2. 20155223 2016-2017-2《Java程序设计》课程总结

    20155223 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1 预备作业2 预备作业3 第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 ...

  3. 20155217 实验四《Java面向对象程序设计》实验报告

    20155217 实验四<Java面向对象程序设计>实验报告 一.实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局 ...

  4. c++ 创建单项链表

    建立单向链表 头指针Head 插入结点 //建立头结点 Head Head=p= malloc(sizeof( struct stu_data)); // memset(stu,,sizeof( st ...

  5. MSP430的CAN通信发送

    1. 电路图如下,RE是接收使能,DE是发送使能,看图的话,这个CAN只支持半双工 2. 使用MSP430F149,以下代码只有发送,其实用的是串口 #include <msp430x14x.h ...

  6. idea 安装后需要手动设置 64位的vmoptions (为了更好的性能和体验)

  7. 我们一起学习WCF 第四篇单通讯和双向通讯

    前言:由于个人原因很久没有更新这个系列了,我会继续的更新这系列的文章.这一章是单向和双向通讯.所谓的单向就是只有发送却没有回复,双向是既有发送还有回复.就是有来无往代表单向,礼尚往来表示双向.下面我用 ...

  8. Maven学习(十二)-----Maven POM

    Maven POM POM代表项目对象模型.它是 Maven 中工作的基本单位,这是一个 XML 文件.它始终保存在该项目基本目录中的 pom.xml 文件.POM 包含的项目是使用 Maven 来构 ...

  9. PHP原生代码写的微信扫码支付实例

    一款PHP原生代码写的微信扫码支付,不基于任何框架,完全手写. 扫码支付只要授权域名对就OK,本地是无法测试.跟openid也没有关系,所以跟支付授权目录页没关系. 微信商户信息配置地址:weixin ...

  10. Google Chrome插件分享

    前言 浏览器是大家日常使用最多的工具之一,对于程序员来说,Google Chrome浏览器当然是大家优选的最爱之一.面对Chrome丰富的插件真的是爱不释手,如何把自己的Chrome调教成自己心仪的样 ...