iOS Swift5从0到1系列(十二):走入 UICollectionView(一):了解篇

一、前言

我面过许多 iOS 开发者,其中有个问题,我偶尔会问到(如果与候选者聊的愉快,我会问这个小问题,回答的不好不会影响最终的结果,回答的好可以加分):

大家可能或多或少有点了解:UITableView 能干的事,UICollectionView 几乎都能干(总有干不了的,除非自定义,例如:删除手势),那么,你认为 UITableView 与 UICollectionView 分别应该在哪些场景用到?

但实际上,并没有标准的答案,而我也只有个人建议给出,主要取决于需求(嗯,我也知道是废话):

  • 如果是瀑布流,那么,老老实实的使用 UICollectionView;
  • 如果是简单的单行列表,那么首选肯定是 UITableView;

总的来说就是:单列用 UITableView,多列或复杂逻辑就用 UICollectionView;

那啥,有人会和我扛,UITableView 单行也能多列!嗯,那你就违背了『用合适的控件使合适的事情,即复杂问题简单化』。

简单看一下,两者的 UI 区别:

52f43bb813ff4dbf9f1827fe04976df0_tplv-k3u1fbpfcp-watermark.png

从上图可以看出:

  • UITableView 默认含有表头、表尾;而 UICollectionView 默认没有;
  • UITableView 默认是 Section + (Header + UITableViewCell + Footer);
  • UICollectionView 默认是 Section +(Header + UICollectionViewCell + Footer);

二、UICollectionView 官方流程图

2.png

上图是官方的流程图:

  • 最上面是一个 N行M列 的 Cell 单元格,我们叫作: UICollectionViewCell;
  • 类似 UITableView,UICollectionView 是 UICollectionViewCell 的容器;
  • UICollectionViewCell 的展示方式是由左侧的 layout(attributes) 来决定,官方提供了一个已经定义好的 layout:UICollectionViewFlowLayout (流式布局),我们也可以自定义;
  • 同样,类似 UITableView,cell 的数据来源由 dataSource 负责,cell 的操作由 delegate 负责;

对于 layout ,你可以类比 delegate 或者 data source,即 cell 的展示由 layout 来决定。

三、UICollectionViewFlowLayout 核心属性源码分析

@available(iOS 6.0, *)
open class UICollectionViewFlowLayout : UICollectionViewLayout {
    // 同一组当中:
    // 垂直方向:行与行之间的间距 or 水平方向:列与列之间的间距
    open var minimumLineSpacing: CGFloat
    // 垂直方向:同一行中,cell之间的间距 or 水平方向:同一列中,cell与cell之间的间距
    open var minimumInteritemSpacing: CGFloat
    // 每个 cell 的尺寸
    open var itemSize: CGSize
    // 滑动方向:默认滑动方向是垂直方向滑动
    open var scrollDirection: UICollectionView.ScrollDirection
    // 每一组头视图的尺寸。如果是垂直方向滑动,则只有高起作用 or 如果是水平方向滑动,则只有宽起作用
    open var headerReferenceSize: CGSize
    // 同上,作用于尾视图
    open var footerReferenceSize: CGSize
    // 每一组的内容缩进
    open var sectionInset: UIEdgeInsets

    // 下面两属性,类似 UITableView,就是我们常说的,是否:吸顶 / 吸底
    @available(iOS 9.0, *)
    open var sectionHeadersPinToVisibleBounds: Bool
    @available(iOS 9.0, *)
    open var sectionFootersPinToVisibleBounds: Bool
}

上面的源码注释已经写的很详细了,后续的组件开发中会用到,这里大家只需要了解即可。

四、UICollectionView 源码简要分析

@available(iOS 6.0, *)
open class UICollectionView : UIScrollView, UIDataSourceTranslating {
    // 给定 frame 大小 && layout(布局)
    public init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
    ......
    // 布局(可以使用默认的 UICollectionViewFlowLayout,也可以自定义)
    open var collectionViewLayout: UICollectionViewLayout
    // 委托:操作 UICollectionViewCell(item)
    weak open var delegate: UICollectionViewDelegate?
    // 数据源
    weak open var dataSource: UICollectionViewDataSource?

    // 注册 cell 的不同方式:手写代码 or XIB    
    open func register(_ cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
    open func register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String)

    // 同上,注册 header / footer 的不同方式
    open func register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
    open func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)

    // 重用 cell、header / footer(根据传入不同的 identifier)
    open func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell
    open func dequeueReusableSupplementaryView(ofKind elementKind: String, withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionReusableView
}

五、UICollectionViewDataSource 源码必要方法分析

public protocol UICollectionViewDataSource : NSObjectProtocol {
    // 我们可能有多个 Section,每个 Section 又可以有不同个数的 Items,即我们的数据结构是二维:[Section][Item]
    @available(iOS 6.0, *)
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

    // 必需调用 -dequeueReusableCellWithReuseIdentifier:forIndexPath: 来获取可重用的 cell,
    // 然后根据 indexPath 来重新赋值,更新显示,最后返回该 cell
    @available(iOS 6.0, *)
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

    // 可选方法,如果只有 1 个 section,可以不用实现(官方文档:默认返回值是 1)
    @available(iOS 6.0, *)
    optional func numberOfSections(in collectionView: UICollectionView) -> Int

    // 可选方法,类同上面第二个方法
    // 必需调用 -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: 来获取可重用的 header / footer
    @available(iOS 6.0, *)
    optional func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
}

你可能感兴趣的:(iOS Swift5从0到1系列(十二):走入 UICollectionView(一):了解篇)