iOS UICollectionView简介 —— HERO博客

来源:互联网 发布:房天下 源码 编辑:程序博客网 时间:2024/04/30 13:39

UICollectionView简介:

UICollectionViewiOS6引进的API,继承UIScrollView,可以自定义布局展示集合视图,布局灵活,可以多列布局

UICollectionView属性

UICollectionViewDataSourceUICollectionViewDelegateUICollectionViewDelegateFlowLayout:代理协议

cell:展示的内容,可复用,在viewDidLoad方法中注册

supplementary view:追加视图,可复用,在viewDidLoad方法中注册

decoration view:背景视图

itemSize:全局cell尺寸,单独定义用代理方法

minimumLineSpacing:全局行间距

minimumInteritemSpacing:全局cell间距

scrollDirection:滚动方向

headerReferenceSize:全局页眉尺寸

footerReferenceSize:全局页脚尺寸

sectionInset:全局区内边距

UICollectionView使用:

//创建布局

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];


//滚动方向

[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];


//组间距

flowLayout1.sectionInset =UIEdgeInsetsMake( , , , );


//注册cell

- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier

- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier


//注册supplementary view

- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier

- (void)registerNib:(nullableUINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier


//创建cell

- (__kindofUICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath


//创建supplementary view

- (__kindofUICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath


#pragma mark <UICollectionViewDataSource>

//组数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView


//cell个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section


//cell内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath


//collectView大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath


//cell的最小行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section


//collectViewmargin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section


//cell的最小列间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section


//设置顶部的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section


//追加视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath


//点击方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath


//是否可以被选择

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath


UICollectionViewLayout简介:

UICollectionViewLayout为UICollectionView提供布局信息,根据需求,我们通常需要自定义一个继承UICollectionViewLayout的类,重载它的方法,达到我们需要的布局。

UICollectionViewLayout重载方法:

//当边界发生改变时,是否应该刷新布局,返回YES表示刷新。

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds


//collectionView内容的尺寸

-(CGSize)collectionViewContentSize


//rect中的所有的元素的布局属性(cell、追加视图、装饰视图)

-(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

3 0