UICollectionView使用笔记

一、与UITableView的相同点与不同点

1、相同点

1)都是显示集合数据

2)都有单元格重用机制,在使用之前都要注册cell(原型cell除外)

3)都需要三个数据源代理方法(组数、行数(item数)、每一个单元格)

2、不同点:

1)UICollectionView不知道如何布局单元格,在实例一个UICollectionView的时候,要传入布局信息。详细见布局对象的介绍。

3、UICollectionView将其单元格的位置,大小和外观的控制权委托给一个单独的布局对象。通过提供一个自定义布局对象,你几乎可以实现任何你能想象到的布局。

4、一切布局相关的设置请到布局对象里设置。


二、UICollectionViewLayout对象介绍

1、在创建UICollectionView的时候必须传入布局对象,但是不能传入UICollectionViewLayout对象,否则不会显示单元格,因为UICollectionViewLayout不具有具体的布局效果,只是布局对象的抽象基类。

2、布局对象必须继承自UICollectionViewLayout或者UICollectionViewFlowLayout。

3、UICollectionViewLayout有一个属性:UICollectionView *collectionView,获取当前需要布局的UICollectionView。

三、UICollectionViewFlowLayout

1、FlowLayout有几个常用的属性:

	CGFloat minimumLineSpacing;//最小行间距
	CGFloat minimumInteritemSpacing;//最小item间距
	CGSize itemSize;//item的大小
	CGSize estimatedItemSize //估计item的大小
	UICollectionViewScrollDirection scrollDirection; //滚动方向
	CGSize headerReferenceSize;//组头大小。设置大小时,只有高度起作用
	CGSize footerReferenceSize;//同上
	UIEdgeInsets sectionInset;//组内边距
	BOOL sectionHeadersPinToVisibleBounds//组头悬停
        BOOL sectionFootersPinToVisibleBounds//组尾悬停


2、prepareLayout方法(继承自prepareLayout)
这个方法使用注意:
//每次布局之前会调用一次
// The collection view calls -prepareLayout once at its first layout as the first message to the layout instance.

//当时布局失效后重新布局的时候回调用一次
// The collection view calls -prepareLayout again after layout is invalidated and before requiring the layout information.

//重写这个方法必须调用 super 方法
// Subclasses should always call super if they override.

系统在第一次实例化一个布局对象时,会调用prepareLayout,在使用prepareLayout时,首先要调用[super prepareLayout]。
四个边距、单元格大小、滚动方向等等在布局对象中设置。



     四个间距:
     1. contentInset:属于scrollView的内边距

     2. sectionInset:属于section(组)的内边距

     3. LineSpacing:属于两行之间的间距,换行时候才有

     4. InteritemSpacing:属于两个item之间的间距

minimumLineSpacing:最小行间距,默认值是10。最小行间距的设置只是设置一个间距的最小值,UICollectionView在布局的时候会根据实际情况调整间距大小,前提是>=minimumLineSpacing

四、自定义UICollectionViewCell的三种方法

1、纯代码方法,继承自UICollectionViewCell,重写initWithFrame方法

2、xib方法:使用UICollectionViewCell控件。要设置重用id。注册时使用registerNib。

3、原型cell。

当视图从storyboard 或 xib 文件创建的时候,会调用awakeFromNib 方法,在这个方法里面,可以创建子控件


五、设置、自定义组头组尾方法
1、代码中设置组头组尾

	//1. 使用之前要注册组头、组尾,注册的时候注意要注册UICollectionReusableView或其子类。并且使用系统提供的常量注册组头或组尾的类型:UICollectionElementKindSectionHeader |  UICollectionElementKindSectionFooter
	
	[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:viewId];
	
	[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:viewId];
	
	//2. 设置组头、组头的尺寸,高度起作用
	
	flowLayout.headerReferenceSize = CGSizeMake(100, 100);
	flowLayout.footerReferenceSize = CGSizeMake(10, 10);
	
	//3. 设置是否 “钉”顶部或底部
	
	flowLayout.sectionHeadersPinToVisibleBounds = YES;
	flowLayout.sectionFootersPinToVisibleBounds = YES;
	
	//4. 重写代理方法
	
	// 不管获取的还是组头还是组尾都会调用这个方法
	- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
	    if (kind == UICollectionElementKindSectionHeader) {
	        HMheaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headId forIndexPath:indexPath];
	        headView.backgroundColor = [UIColor whiteColor];
	        return headView;
	    }
	    else{
	        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footId forIndexPath:indexPath];
	        footView.backgroundColor = [UIColor blueColor];
	        return footView;
	    }
}


2、自定义组头组尾

1) 纯代码:继承自UICollectionReusableView

2)xib:拖一个UICollectionReusableView,设置重用ID

3、原型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值