Loading [MathJax]/jax/output/CommonHTML/config.js
社区首页 >专栏 >iOS---UICollectionView详解和常用API翻译 UICollectionView
用法类似于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,
常用属性
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) CGSize estimatedItemSize
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;
@property (nonatomic) CGSize headerReferenceSize;
@property (nonatomic) CGSize footerReferenceSize;
@property (nonatomic) UIEdgeInsets sectionInset;
UICollectionViewDelegateFlowLayout代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
- (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类。
重写方法
-(CGSize)collectionViewContentSize
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
-(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath
-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath
-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath
-(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
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(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
本文参与
腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-08-28 ,如有侵权请联系
cloudcommunity@tencent.com 删除