ui–CollectionViewCell

  • Post author:
  • Post category:其他


.介绍



集合视图

,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






[kind


isEqualToString


:

UICollectionElementKindSectionHeader

]

cell

整体边距


9)

– (

UIEdgeInsets

)collectionView:(

UICollectionView

*)collectionView layout:(

UICollectionViewLayout

*)collectionViewLayout insetForSectionAtIndex:(

NSInteger

)section





描述的是所有cell相对于另一个视图上、左、下、右的距离


补充:

[[UIDevice currentDevice].systemVersion floatValue]





获取系统版本号



版权声明:本文为qq_30211165原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。