美文网首页
UICollectionView

UICollectionView

作者: mdiep | 来源:发表于2016-03-27 14:55 被阅读894次

UICollectionView视图是iOS6之后加入到Apple API中的,它是一种更为灵活也更为强大的布局控件。collection view自己提供了一个类(UICollectionViewLayout)来进行布局,我们一般用它的子类(UICollectionViewFlowLayout),Collection view 和 table view是十分类似的控件,但是table view仅仅只有系统提供的两种样式,所以要实现某些效果,就需要非常的麻烦,但是利用Collection view实现就非常的简单。如下图所示,UICollectionView主要分为三个部分.

  • cell部分
  • decoration view 背景视图部分
  • supplementary view 视图追加部分(header,footer view)
UICollectionView布局.PNG

UICollectionViewUITableView一样使用了重用的的机制,需要在创建UICollectionView的时候进行注册cell


创建和获取cell
  • 注册cellview
    - registerClass:forCellWithReuseIdentifier:
    - registerNib:forCellWithReuseIdentifier:

  • 注册 supplementaryview
    - registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
    - registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

  • 获取cell/supplementary view
    - dequeueReusableCellWithReuseIdentifier:forIndexPath:
    - dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

UITableView一样,UICollectionView也需要实现两个代理delegatedataSource

代理方法 - UICollectionViewDelegate

参考Apple文档 - UICollectionViewDelegate

  • 常用的几个方法
    - collectionView:didSelectItemAtIndexPath: - 选中后调用
    - collectionView:didDeselectItemAtIndexPath: - 重选中到非选中过程调用
    - collectionView:willDisplayCell:forItemAtIndexPath: - 同下,先于其执行
    - collectionView:didEndDisplayingCell:forItemAtIndexPath: - cell显示或者消失调用,配合collection view的addition和removal行为使用
数据源 - UICollectionViewDataSource

参考Apple 文档 - UICollectionViewDataSource

数据源中最少要实现如下两个方法

- numberOfSectionsInCollectionView: - 返回每个section中item的数量
- collectionView:cellForItemAtIndexPath: - 返回指定index path中的cell

  • 常用的几个方法

- collectionView:numberOfItemsInSection: - 告诉collection view当前分区有个item
- numberOfSectionsInCollectionView: - 告诉colleciton view 有几个分区
- collectionView:cellForItemAtIndexPath: - 返回cell
- collectionView:viewForSupplementaryElementOfKind:atIndexPath: - 返回追加视图(header/footer view)
- collectionView:canMoveItemAtIndexPath: - 告诉collection view 是否可以删除
- collectionView:moveItemAtIndexPath:toIndexPath: - 移动一个item到另一个位置

布局 - UICollectionViewFlowLayout

参考Apple文档 - UICollectionViewFlowLayout

layout对象的属性

scrollDirection - 滚动方向(枚举值提供)
minimumLineSpacing - 两行/两列(取决于竖直还是水平滚动)之间的最小间距
minimumInteritemSpacing - 同行/同列之间两个item之间的最小距离
itemSize - 每个item的大小
estimatedItemSize - 预估item的大小
sectionInset - 每个section显示content的大小

headerReferenceSize - header view的size(如果是竖直滚动,则size.width = collectionView.frame.size.width)
footerReferenceSize - footer view的size(如果是竖直滚动,则size.width = collectionView.frame.size.width)

sectionHeadersPinToVisibleBounds(Available in iOS 9.0 and later) - 滚动的时候固定section header view 不动,直到下一个section header view到达最顶部,先前的header view 移除 
sectionFootersPinToVisibleBounds(Available in iOS 9.0 and later)  - 与上同理
布局代理 - UICollectionViewDelegateFlowLayout

参考 - Apple文档 - UICollectionViewDelegateFlowLayout

主要是以代理的方式实现了前面几个属性的功能,如果有代理,以代理中的方法返回值为最终获取值(代理返回值覆盖设置的属性值)具体的代理方法略

相关文章

网友评论

      本文标题:UICollectionView

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