一.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. echo、print、print_r、var_dump

    echo(): 可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print(): 函数pri ...

  2. django的Cookie-9

    设置Cookie 可以通过HttpResponse对象中的set_cookie方法来设置cookie. HttpResponse.set_cookie(cookie名字, value=cookie值, ...

  3. SQL注入总结篇

    分类SQL注入的攻击方式根据应用程序处理数据库返回内容的不同,可以分为可显注入.报错注入和盲注. 可显注入攻击者可以直接在当前界面内容中获取想要获得的内容. 报错注入数据库查询返回结果并没有在页面中显 ...

  4. [BZOJ3218]a + b Problem-[主席树+网络流-最小割]

    Description 传送门 Solution 此处我们按最小割的思路考虑. 暴力:S->i表示该点选黑色的权值b[i]:i->T表示该点选白色的权值w[i].考虑如果某个点i受点j为白 ...

  5. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

  6. js的视频和音频采集

    js的视频和音频采集 今天要写的,不是大家平时会用到的东西.因为兼容性实在不行,只是为了说明下前端原来还能干这些事. 大家能想象前端是能将摄像头和麦克风的视频流和音频流提取出来,再为所欲为的么.或者说 ...

  7. Yii2.0 高级模版编写使用自定义组件(component)

    翻译自:http://www.yiiframework.com/wiki/760/yii-2-0-write-use-a-custom-component-in-yii2-0-advanced-tem ...

  8. Html+CSS 学习第二天

    趁着这两天,将html和CSS基本上学了一遍,大家如果想学习的话,可以百度w3cSchool,进行学习. 基础我就不说了,直接将我做的一个登陆页面放上去.刚学完CSS,写个漂亮的登录界面恶心死我了,感 ...

  9. React入门基础(学习笔记)

    这篇博客是我通过阅读React官方文档的教程总结的学习笔记,翻译可能存在误差,如有疑问请参见http://reactjs.cn/react/docs/tutorial.html . 一.所需文件 在编 ...

  10. katalon系列十三:5.10新增跳过用例&命令行赋值全局变量

    Katalon Studio升级到5.10版本了,这次新增了2个很实用的功能:一.跳过用例在Listener中新增了跳过用例方法,Listener类似于JUnit4的annotation中的@Befo ...