FlexibleCollectionViewController 2.0.4

FlexibleCollectionViewController 2.0.4

测试已测试
语言语言 SwiftSwift
许可 MIT
发布日期最新版本发布日期2016年10月
SwiftSwift 版本3.0
SPM支持 SPM

Dima Pilipenko 维护。



FlexibleCollectionViewController

一个 Swift 库,它提供了通用的集合视图控制器,具有外部数据处理功能,例如确定与 indexPath 相关的 cell 的 reuseIdentifier,配置用于显示的 cell 和 cell 选择处理器等

示例

使用配置进行初始化

_flexibleCollectionVC = FlexibleCollectionViewController(collectionViewLayout: CustomFlowLayout(), configuration: CollectionConfiguration(userInteractionEnabled: true, showsHorizontalScrollIndicator: false, isScrollEnabled: true, multipleTouchEnabled: false, backgroundColor: .clear))

Cell 和补充视图注册

_flexibleCollectionVC.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "UICollectionViewCell")
_flexibleCollectionVC.registerSupplementaryView(UIHeaderImageCollectionView.self, kind: .header, reuseIdentifier: UIHeaderImageCollectionView.reuseIdentifier)

请求特定 indexPath 的 cell 的 identifier

_flexibleCollectionVC.requestCellIdentifier = { value in
  return "UICollectionViewCell"
}

请求特定 indexPath 的补充视图的 identifier

_flexibleCollectionVC.requestSupplementaryIdentifier = { value in
  return UIHeaderImageCollectionView.reuseIdentifier
}

使用与 indexPath 相关的数据配置输入 cell

_flexibleCollectionVC.configureCell = { (cell: UICollectionViewCell, data: CollectionImageCellData?, indexPath: IndexPath) in
  guard let data = data else {
    return false
  }

  cell.backgroundColor = data.color

  return true
}

使用与 indexPath 相关的种类和数据配置补充视图

_flexibleCollectionVC.configureSupplementary = { (view: UICollectionReusableView, kind: SupplementaryKind, data: CollectionImageCellData?, indexPath: IndexPath) in
  if let view = view as? UIHeaderImageCollectionView, let data = data {
    view.text = data.category

    return true
  }

  return false
}

处理 cell 选择与相关 indexPath

_flexibleCollectionVC.cellDidSelect = { value in
  return (deselect: true, animate: true)
}

估算 cell 的大小

_flexibleCollectionVC.estimateCellSize = { value in
  guard let layout = value.1 as? UICollectionViewFlowLayout else {
    return nil
  }

  let col: CGFloat = 3
  let width = value.0.bounds.width-(layout.sectionInset.left+layout.sectionInset.right)
  let side = round(width/col)-layout.minimumInteritemSpacing
  return CGSize(width: side, height: side)
}

将预定义的 cell 按照与 ListGenerator 相关的生成顺序放置

_flexibleCollectionVC.setData(getData())

要求

Swift 3 或以上

安装

FlexibleCollectionViewController 可通过 CocoaPods 获取。要安装,只需在 Podfile 中添加以下行

pod "FlexibleCollectionViewController"

作者

Pilipenko Dima,[email protected]

许可证

FlexibleCollectionViewController 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。