.介绍
集合视图,iOS6之后出现的视图控件,与UITableView类似,最大的特点是可以对cell实现灵活的布局
UICollectionView使用的cell为UICollectionViewCell
注意:self.automaticallyAdjustsScrollViewInsets =NO;
2.UICollectionViewFlowLayout
介绍:UICollectionViewFlowLayout系统提供的,能够实现cell的网格布局
UICollectionViewFlowLayout的父类是UICollectionViewLayout,UICollectionViewLayout集合视图的布局类,是一个抽象基类
1)@property (nonatomic)UICollectionViewScrollDirection scrollDirection
设置collectionView的方向
UICollectionViewScrollDirectionVertical 竖直
UICollectionViewScrollDirectionHorizontal 水平
2)@property (nonatomic)CGFloat minimumLineSpacing;
设置横向的最小间隔
3)@property (nonatomic)CGFloat minimumInteritemSpacing;
设置纵向的最小间隔
注意:如果CollectionView垂直滚动,横向的最小间隔能直接生效;纵向的最小间隔由实际间隔需要根据item的size和edgesInsets的值来调节
3.UICollectionView基本方法和属性
1)- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
利用布局对象,实现集合视图的初始化
2)@property (nonatomic,assign) id <UICollectionViewDelegate> delegate
设置代理UICollectionViewDelegateFlowLayout
3)@property (nonatomic,assign) id <UICollectionViewDataSource> dataSource;
设置代理UICollectionViewDataSource
4.UICollectionView代理方法
cell的方法
1)- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
指定每一个item的大小
2)- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
一个section有多少个Item(cell)
3)- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
返回Cell
(1)Cell的注册方法
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier
(2)Cell取出的方式
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath
通过重用标识符和indexPath到UICollectionView的重用队列中获取cell对象,因为已经提前注册了cell,如果获取不到cell对象,UICollectionView会根据注册的cell样式,自动创建cell对象
4)- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
cell被选中的时候调用的方法
section的方法
5)- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
设置组的个数
6)- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
设置顶部view的高度。如果垂直滚动,view的width与集合视图一致,需要设置高度
7)- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
设置底部view的高度
8)- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
通过此方法为section的headerView和footerView赋值
(1)注册方法
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
注册的种类
UICollectionElementKindSectionHeader
设置sectionHeaderView
UICollectionElementKindSectionFooter
设置SectionFooterView
(2)取出的方式
- (id)dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath
通过此方法为section的headerView和footerView赋值
用下面方法判断是header还是footer
[kindisEqualToString:UICollectionElementKindSectionHeader]
cell整体边距
9)- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
描述的是所有cell相对于另一个视图上、左、下、右的距离
补充:[[UIDevice currentDevice].systemVersion floatValue]
获取系统版本号