iOS---UICollectionView详解和经常使用API翻译

UICollectionView

  • 1.必需要设置布局参数
  • 2.注册cell
用法相似于UITableView 类。自动实现重用,必须注册初始化。
使用UICollectionView必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

Collection View的构成,咱们能看到的有三个部分:

  • Cells
  • Supplementary Views 追加视图 (相似Header或者Footer)
  • Decoration Views 装饰视图 (用做背景展现)

1、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,所以须要这个尺寸来配置滚动行为。

2、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 (执行选择事件)
*/
相关文章
相关标签/搜索