UICollectionView使用笔记
来源:互联网 发布:mac os怎么卸载软件 编辑:程序博客网 时间:2024/06/11 01:08
1、相同点
1)都是显示集合数据
2)都有单元格重用机制,在使用之前都要注册cell(原型cell除外)
3)都需要三个数据源代理方法(组数、行数(item数)、每一个单元格)
2、不同点:
1)UICollectionView不知道如何布局单元格,在实例一个UICollectionView的时候,要传入布局信息。详细见布局对象的介绍。
3、UICollectionView将其单元格的位置,大小和外观的控制权委托给一个单独的布局对象。通过提供一个自定义布局对象,你几乎可以实现任何你能想象到的布局。
4、一切布局相关的设置请到布局对象里设置。二、UICollectionViewLayout对象介绍
1、在创建UICollectionView的时候必须传入布局对象,但是不能传入UICollectionViewLayout对象,否则不会显示单元格,因为UICollectionViewLayout不具有具体的布局效果,只是布局对象的抽象基类。
2、布局对象必须继承自UICollectionViewLayout或者UICollectionViewFlowLayout。
3、UICollectionViewLayout有一个属性:UICollectionView *collectionView,获取当前需要布局的UICollectionView。三、UICollectionViewFlowLayout
1、FlowLayout有几个常用的属性:
CGFloat minimumLineSpacing;//最小行间距CGFloat minimumInteritemSpacing;//最小item间距CGSize itemSize;//item的大小CGSize estimatedItemSize //估计item的大小UICollectionViewScrollDirection scrollDirection; //滚动方向CGSize headerReferenceSize;//组头大小。设置大小时,只有高度起作用CGSize footerReferenceSize;//同上UIEdgeInsets sectionInset;//组内边距BOOL sectionHeadersPinToVisibleBounds//组头悬停 BOOL sectionFootersPinToVisibleBounds//组尾悬停
这个方法使用注意:
//每次布局之前会调用一次// The collection view calls -prepareLayout once at its first layout as the first message to the layout instance.//当时布局失效后重新布局的时候回调用一次// The collection view calls -prepareLayout again after layout is invalidated and before requiring the layout information.//重写这个方法必须调用 super 方法// Subclasses should always call super if they override.
系统在第一次实例化一个布局对象时,会调用prepareLayout,在使用prepareLayout时,首先要调用[super prepareLayout]。
四个边距、单元格大小、滚动方向等等在布局对象中设置。
四个间距:
1. contentInset:属于scrollView的内边距
2. sectionInset:属于section(组)的内边距
3. LineSpacing:属于两行之间的间距,换行时候才有
4. InteritemSpacing:属于两个item之间的间距
minimumLineSpacing:最小行间距,默认值是10。最小行间距的设置只是设置一个间距的最小值,UICollectionView在布局的时候会根据实际情况调整间距大小,前提是>=minimumLineSpacing
四、自定义UICollectionViewCell的三种方法
1、纯代码方法,继承自UICollectionViewCell,重写initWithFrame方法
2、xib方法:使用UICollectionViewCell控件。要设置重用id。注册时使用registerNib。
3、原型cell。
当视图从storyboard 或 xib 文件创建的时候,会调用awakeFromNib 方法,在这个方法里面,可以创建子控件
五、设置、自定义组头组尾方法
1、代码中设置组头组尾
//1. 使用之前要注册组头、组尾,注册的时候注意要注册UICollectionReusableView或其子类。并且使用系统提供的常量注册组头或组尾的类型:UICollectionElementKindSectionHeader | UICollectionElementKindSectionFooter[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:viewId];[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:viewId];//2. 设置组头、组头的尺寸,高度起作用flowLayout.headerReferenceSize = CGSizeMake(100, 100);flowLayout.footerReferenceSize = CGSizeMake(10, 10);//3. 设置是否 “钉”顶部或底部flowLayout.sectionHeadersPinToVisibleBounds = YES;flowLayout.sectionFootersPinToVisibleBounds = YES;//4. 重写代理方法// 不管获取的还是组头还是组尾都会调用这个方法- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if (kind == UICollectionElementKindSectionHeader) { HMheaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headId forIndexPath:indexPath]; headView.backgroundColor = [UIColor whiteColor]; return headView; } else{ UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footId forIndexPath:indexPath]; footView.backgroundColor = [UIColor blueColor]; return footView; }}
1) 纯代码:继承自UICollectionReusableView
2)xib:拖一个UICollectionReusableView,设置重用ID
3、原型
- UICollectionView使用笔记
- UICollectionView使用(WWDC session学习笔记)
- UICollectionView 笔记
- UICollectionView使用
- UICollectionView 使用
- UICollectionView使用
- UICollectionView使用
- iOS学习笔记6- iOS UICollectionView简单使用
- UICollectionView笔记一:BASIC
- UICollectionView笔记-1
- UICollectionView笔记-2
- 关于UICollectionView一些笔记
- 归纳笔记010:UICollectionView
- iOS UICollectionView笔记
- UICollectionView笔记1
- UICollectionView笔记2
- UICollectionView基础API笔记
- IOS6- UICollectionView的使用
- 技巧118 对你的工作进行拼写检查
- 最简单的验证码(利用JSP生成验证码)
- sqlite 数据库的基本命令
- leetcode 二叉树最小深度
- 如何能更好的面试和被面试?
- UICollectionView使用笔记
- E. Ants in Leaves
- 设计模式的六大原则
- hdu4430 Yukari's Birthday(二分)
- Leetcode 292. Nim Game
- innerHTML 概念
- MySQL 表和列的注释的添加以及查看
- XML解析之JAXP
- overflowmenu的样式设置