UICollectionView 笔记

作者: David_Cap | 来源:发表于2015-08-04 15:03 被阅读6189次

UICollectionView 笔记

Collection View Basics

CollectionView和tableView有几分相似却又有不同。

那什么是collectionView呢。你看过苹果自带的相册吗那个就是collectionView。就是一个个cell组成的展示的容器

组成

首先要理解一下他的组成,并与tableView进行比较。

  1. Delegate DataSource

    它和tableView一样,都是由delegate和dataSource驱动的。它需要的协议是,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout。而这部分和tableView一样是负责定义cell和监听cell的一系列的问题。

  2. CollectionView layout

    这部分是tableView 所没有的,这部分是复杂cell的布局。例如是水平展示还是垂直,每个Cell间隔多少...... Something like that

  3. 性质方面

    在CollectionView中它和tableView不一样,tableView是一排垂直的东西,就那么直直的展示下来,而CollectionView是一个个的小cell(细胞)。

![Uploading ds_data_object_layout_2x_766033.png . . .]


cv_objects_2x.png

Designing Your Data Source and Delegate

存放数据的容器结构

第一层是一个NSMutableArray里面 add 例外一些 NSMutableArray的数组(这就是第二层数据)。第3层才是存放正真的数据。如图。

ds_data_object_layout_2x.png

Data Source and Delegate

定义 一共有多少的大section
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

定义 每个section中有多少个cell(item)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section]

定义每个UICollectionView 的大小 (可以不用代理写)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

定义每个UICollectionView 的 margin(内边距)(可以不用代理写)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

UICollectionView被选中时调用的方法 相当于 tableView didSelect
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

这个是最重要的方法,定义了每个cell要现实什么数据
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

创建 Collection View

创建当然是用alloc啦,来看看

 self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];

然后就看蒙了,collectionViewLayout是什么鬼。还记得之前介绍的layout吗,这个就是。

然后就是在alloc之前 先创建一个 collectionViewLayout

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

这个layout里面还是有几个数据要介绍一下的。

minimumLineSpacing //The minimum spacing to use between lines of items in the grid.
也就是cell与cell的上下之间的最小间距。 CGFloat

minimumInteritemSpacing //The minimum spacing to use between items in the same row.
cell与cell之间的横向最小间距。 CGFloat

itemSize //The default size to use for cells.
cell的size CGSize

estimatedItemSize //The estimated size of cells in the collection view.
先给collectionViewCell先估算个一个高度,然后在代理方法中再返回真实的size

sectionInset  //The margins used to lay out content in a section
这个就是设置cell与cell的内边距的东西   UIEdgeInsets

创建总结

也就是第一步 你要创建个 layout对象,赋予一些 基本的值。然后再alloc一个Collection View。然后设置delegate等。。。

自定义cell

alloc好了Collection View,那么就是要自定义一些cell去show数据。和tableView不同的是这里要多一个步骤。例如的我 UICollectionViewCell的子类的名字叫 CollectionViewTitleCell。那么就要如下:

//注册子cell
 [_collectionView registerClass:[VSTitleOfSecondCategoryCell class] forCellWithReuseIdentifier:@"CollectionViewTitleCell"];
 
//然后在cellForItemAtIndexPath中这么写
//取缓存并强制转换
  VSTitleOfSecondCategoryCell *cell = (VSTitleOfSecondCategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

然后就可以给cell 赋值啊定义的啊........

参考致谢

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CollectionViewBasics/CollectionViewBasics.html#//apple_ref/doc/uid/TP40012334-CH2-SW1
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/index.html#//apple_ref/occ/instp/UICollectionViewFlowLayout/sectionInset

相关文章

网友评论

  • 新地球说着一口陌生腔调:你好 xib中怎么在collectionview 设置一个view 作为header呢?
  • 长鲜:sectionInset是指 sectionInset 不是cellInset
  • Caiflower:estimatedItemSize这个属性应该是类似tableView的estimatedRowHeight(估算高度),然后再代理方法中返回真正的行高,那么同理这个也是先给collectionViewCell先估算个一个高度,然后在代理方法中再返回真实的尺寸
    David_Cap:@花菜ChrisCai 谢谢 提示,纠正。已经修改。
    Caiflower:@花菜ChrisCai estimatedItemSize估算尺寸,写成估算高度了.
  • 两年如歌:受用 哥们是大学刚毕业么?
    David_Cap:@丹尼i 是啊,还没毕业 :joy: ,2016年 6月份 毕业。

本文标题:UICollectionView 笔记

本文链接:https://www.haomeiwen.com/subject/yivvqttx.html