IOS UICollectionView

UICollectionView
1.必须要设置布局参数
2.注册cell
用法类似于UITableView 类。自动实现重用,必须注册初始化。

使用UICollectionView必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

Collection View的构成,我们能看到的有三个部分:
Cells
Supplementary Views 追加视图 (类似Header或者Footer)
Decoration Views 装饰视图 (用作背景展示)
一、UICollectionViewLayout
UICollectionView的精髓
Layout决定了UICollectionView是如何显示在界面上的。在展示之前,一般需要生成合适的UICollectionViewLayout子类对象,并将其赋予CollectionView的collectionViewLayout属性。
1.UICollectionViewFlowLayout
最简单也是最常用的默认layout对象,UICollectionViewFlowLayout。Flow Layout简单说是一个直线对齐的layout,
常用属性

// 行间距,也可以通过collectionView: layout:minimumLineSpacingForSectionAtIndex:
@property (nonatomic) CGFloat minimumLineSpacing;

// 设置cell之间的间距
@property (nonatomic) CGFloat minimumInteritemSpacing;

// 定义每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,
//可以使用-collectionView:layout:sizeForItemAtIndexPath:方法。
@property (nonatomic) CGSize itemSize;

@property (nonatomic) CGSize estimatedItemSize

// 滚动方向,默认是水平
// UICollectionViewScrollDirectionVertical
// UICollectionViewScrollDirectionHorizontal
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;

// 根据滚动方向不同,header和footer的高和宽中只有一个会起作用。
// 垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用,
@property (nonatomic) CGSize headerReferenceSize;
@property (nonatomic) CGSize footerReferenceSize;

// 组间距 缩进
@property (nonatomic) UIEdgeInsets sectionInset;
UICollectionViewDelegateFlowLayout代理方法

// 设定指定Cell的尺寸
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

// 设定collectionView(指定区)的边距
- (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

// 设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

// 设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
2.UICollectionViewLayoutAttributes
一个非常重要的类,
属性列表

// 边框
@property (nonatomic) CGRect frame
// 中心点
@property (nonatomic) CGPoint center
// 大小
@property (nonatomic) CGSize size
// 形状
@property (nonatomic) CATransform3D transform3D
// 透明度
@property (nonatomic) CGFloat alpha
// 层次关系
@property (nonatomic) NSInteger zIndex
// 是否隐藏
@property (nonatomic, getter=isHidden) BOOL hidden
3.自定义的UICollectionViewLayout
UICollectionViewLayout的功能为向UICollectionView提供布局信息.
继承UICollectionViewLayout类。
重写方法

// 返回collectionView的内容的尺寸
-(CGSize)collectionViewContentSize

// 返回rect中的所有的元素的布局属性
/*
返回的是包含UICollectionViewLayoutAttributes的NSArray
UICollectionViewLayoutAttributes可以是cell,追加视图或装饰
视图的信息,通过不同的UICollectionViewLayoutAttributes初始
化方法可以得到不同类型的UICollectionViewLayoutAttributes:
*/
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

// 返回对应于indexPath的位置的cell的布局属性
-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath

//返回对应于indexPath的位置的追加视图的布局属性,如果没有追加视图可不重载
-(UICollectionViewLayoutAttributes )layoutAttributesForSupplementaryViewOfKind:(NSString )kind atIndexPath:(NSIndexPath *)indexPath

// 返回对应于indexPath的位置的装饰视图的布局属性,如果没有装饰视图可不重载
-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath

// 当边界发生改变时,是否应该刷新布局。如果YES则在边界变化(一般是scroll到其他地方)时,将重新计算需要的布局信息。
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
另外需要了解的是,在初始化一个UICollectionViewLayout实例后,会有一系列准备方法被自动调用,以保证layout实例的正确。

首先,-(void)prepareLayout将被调用,

默认下该方法什么没做,但是在自己的子类实现中
,一般在该方法中设定一些必要的layout的结构和初始需要的参数等。
之后,-(CGSize) collectionViewContentSize将被调用,

以确定collection应该占据的尺寸。注意这里的尺寸不是指可视部分的尺寸,而应该是所有内容所占的尺寸。
collectionView的本质是一个scrollView,因此需要这个尺寸来配置滚动行为。
二、UICollectionView
1.UICollectionViewDataSource
①section的数量 -numberOfSectionsInCollection:

②某个section里有多少个item -collectionView:numberOfItemsInSection:

③对于某个位置应该显示什么样的cell -collectionView:cellForItemAtIndexPath:

2.UICollectionViewDelegate
// 当指定indexPath处的item被选择时触发
- (void)collectionView:(UICollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView )collectionView shouldSelectItemAtIndexPath:(NSIndexPath )indexPath;

// 下面是三个和高亮有关的方法:

  • (BOOL)collectionView:(UICollectionView )collectionView shouldHighlightItemAtIndexPath:(NSIndexPath )indexPath

  • (void)collectionView:(UICollectionView )collectionView didHighlightItemAtIndexPath:(NSIndexPath )indexPath

  • (void)collectionView:(UICollectionView )collectionView didUnhighlightItemAtIndexPath:(NSIndexPath )indexPath

/*
事件的处理顺序如下:

1.手指按下
2.shouldHighlightItemAtIndexPath (如果返回YES则向下执行,否则执行到这里为止)
3.didHighlightItemAtIndexPath (高亮)
4.手指松开
5.didUnhighlightItemAtIndexPath (取消高亮)
6.shouldSelectItemAtIndexPath (如果返回YES则向下执行,否则执行到这里为止)
7.didSelectItemAtIndexPath (执行选择事件)
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值