CollectionAndTableViewCompatible 0.2.2

CollectionAndTableViewCompatible 0.2.2

测试已测试
Lang语言 SwiftSwift
许可 Apache-2.0
发布最新发布2019年3月
SPM支持SPM

Jimmie JensenFredrik Nannestad维护。



  • Jimmie Jensen

CollectionAndTableViewCompatible

Version License Platform

CollectionAndTableViewCompatible提供了编写UITableViewDataSource和UICollectionViewDataSource所需的基础代码。实际上,当使用这里提供的类时,大多数情况下您根本不必编写这些实现。

为什么您应该使用CollectionAndTableViewCompatible

无论您是从头创建新的数据源,还是需要重构变得难以维护的旧代码,您可能都会从将CollectionAndTableViewCompatible添加到项目中受益。

如果您对以下问题中的任何一个或多个回答是肯定的,这个项目与您相关

  • 您的UIViewController是数据源。
  • 您喜欢通过分离职责对象来保持视图控制器轻量级。
  • 您不希望编写基础代码。
  • 您的表格或集合视图由布局不同的不同单元格组成。
  • 您想要能够快速更改表格或集合视图及其单元格的布局(例如,以响应设计更改)。
  • 您的表格或集合视图支持重新排序或编辑。
  • 您现有的表格/集合视图数据源与其表示的实体的类型紧密耦合。
  • 您的cellForRowAtIndexPath:通过在indexPath参数上的sectionrow属性上切换来决定返回哪个单元格。
  • 您的cellForRowAtIndexPath:处理单元格的布局。

基础知识

CollectionAndTableViewCompatible 的主要思想是使支持您表格/集合查看的数据模型与数据源协议(UITableViewDataSourceUICollectionViewDataSource)更加兼容。

CollectionAndTableViewCompatible 通过提供两个数据源协议的默认和完全通用的实现来实现这一点,以便对模型(例如包含您模型实体的数组)所做的更改将直接反映在表格/集合视图中。它还提供了一些协议,可以帮助您将布局单元格、重排序和编辑等工作委派给相关对象。

最后,它还提供了一些其他协议,这些协议支持单元格的显示周期(willDisplaydidEndDisplay)以及新的数据源预取协议(UITableViewDataSourcePrefetchingUICollectionViewDataSourcePrefetching)。

示例项目

您可以在 这里 找到展示 CollectionAndTableViewCompatible 使用的示例项目。

CocoaPods

如果您正在使用 CocoaPods,您可以通过向示例项目中添加以下 pod 并在 Example 目录下运行 pod install 来运行示例项目。

需求

  • Swift4
  • iOS 8+
  • Xcode8

对旧版本的支持

  • 在旧分支/插件上支持 Swift3。使用插件版本 0.1.5

请注意,如果从 0.1.x 更新到 0.2.x,其中包含破坏性更改。

安装

Git 子模块

通过在现有 git 仓库根目录下运行以下命令将该项目添加为 git 子模块:

git submodule add https://github.com/jayway/CollectionAndTableViewCompatible.git

在子模块复制后,将 Classes 文件夹中的文件添加到您的 Xcode 项目中(请确保未勾选“如果需要则复制”)。

CocoaPods

CollectionAndTableViewCompatible 也可通过 CocoaPods 使用。要安装,只需将以下行添加到您的 Podfile。

pod "CollectionAndTableViewCompatible"

如何使用

该项目旨在用于基于 storyboards 的应用程序,并使用原型单元格。目前,如果您正在使用 nibs 或您完全在代码中创建 UI,您必须手动将 nibs 或类注册到您的表格/集合视图中。

您需要执行以下步骤来设置表格/集合视图:

  • 确保您的模型对象符合 TableViewCompatible 和/或 CollectionViewCompatible 协议。
  • 创建一个 UITableViewCell 和/或 UIColllectionViewCell 子类,并使它们符合 Configurable 协议。`model` 属性类型应为您的模型对象的类型。
  • 创建 TableViewDataSource 和/或 CollectionViewDataSource 的子类,并通过调用 init(tableView: UITableView) 和/或 init(collectionView: UICollectionView) 进行初始化。
  • 通过提供包含您的模型对象的 TableViewSection/CollectionViewSection 实例来初始化您的数据源实例的 sections 属性。
  • 在您的表格/集合视图中调用 reloadData()

现在您可以在不修改数据源实例的 sections 属性的情况下简单地在表格/集合视图中添加、删除和重新排列段落和单元格,只需更新数据源的 sections 属性并重启表格/集合视图的 reloadData()

要使单元格可移动或可编辑,只需为您的 TableViewCompatible/CollectionViewCompatible 模型对象的 movableeditable 属性返回 true

以下是一个示例,展示了在实现时 TableViewCompatible/CollectionViewCompatibleConfigurable 如何交互:

class MyTableCell: UITableViewCell, Configurable {
    
    var model: MyCellModel?

    func configureWithModel(_ model: MyCellModel) {
        self.model = model
        titleLabel.text = model.title
    }

}

class MyCollectionCell: UICollectionViewCell, Configurable {

    @IBOutlet weak var titleLabel: UILabel!

    var model: MyCellModel?

    func configureWithModel(_ model: MyCellModel) {
        self.model = model
        titleLabel.text = model.title
    }

}

class MyCellModel: TableViewCompatible, CollectionViewCompatible {

    // Your custom properties
    var title: String = "Some title"

    // TableViewCompatible/CollectionViewCompatible conformance
    var reuseIdentifier: String = "MyCellIdentifier"
    var selected: Bool = false
    var editable: Bool = true
    var movable: Bool = true

    // TableViewCompatible
    func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! MyTableCell
        cell.configureWithModel(self)
        return cell
    }

    // CollectionViewCompatible
    func cellForCollectionView(collectionView: UICollectionView, atIndexPath indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionCell
        cell.configureWithModel(self)
        return cell
    }

}

作者

许可

CocoaPods的CollectionAndTableViewCompatible模块受Apache许可证版本2.0的约束。更多详情请参阅LICENSE文件。