EZDataSource 0.5.0

EZDataSource 0.5.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2019年6月
SPM支持 SPM

Brad SmithBob SolorioGuillermo GueriniYang Chen 维护。



  • 作者:
  • ezCater

DataSource

Swift 5.0

DataSource 是一个简洁且与 UI 无关的协议,用于表示数据源。它可以直接使用,但也可以在需要任何定制时提供极大的灵活性。

在核心上,DataSource 是一个简单的协议。它需要一个 ItemType,表示包含对象的类型。

public protocol DataSource {
    associatedtype ItemType

    var reloadBlock: ReloadBlock? { get set }
    var numberOfSections: Int { get }

    func numberOfItems(in section: Int) -> Int
    func item(at indexPath: IndexPath) -> ItemType?
    func indexPath(after indexPath: IndexPath) -> IndexPath?
}

它使用闭包,ReloadBlock,来传递支持数据的 ChangeSet

public typealias ReloadBlock = (ChangeSet) -> Void

ChangeSet 是对相关 UI 元素(例如 UITableViewUICollectionView)执行的一组 Change 操作。

public enum ChangeSet {
    case some([Change])
    case all
}

public enum Change {
    case section(type: ChangeType)
    case object(type: ChangeType)
}

public enum ChangeType {
    case insert(IndexPath)
    case delete(IndexPath)
    case move(IndexPath, IndexPath)
    case update(IndexPath)
}

ListDataSource

ListDataSource 继承自 DataSource 并表示由数组支持的单一部分。

public protocol ListDataSource: DataSource {
    var items: [ItemType] { get }
}

它包括以下默认实现:

  • var numberOfSections: Int
  • func numberOfItems(in section: Int) -> Int
  • func item(at indexPath: IndexPath) -> ItemType?
  • func indexPath(after indexPath: IndexPath) -> IndexPath?

示例

class SimpleDataSource: ListDataSource {
    typealias ItemType = String

    var items = [
        "Item 0",
        "Item 1",
        "Item 2"
    ]

    var reloadBlock: ReloadBlock?
}

分区数据源

分区数据源 继承自 数据源,表示由多个分区组成,每个分区由一个 分区 支持。

public protocol SectionedDataSource: DataSource {
    associatedtype SectionType: Section<ItemType>

    var sections: [SectionType] { get }

    func section(at index: Int) -> SectionType?
    func headerTitle(for section: Int) -> String?
    func footerTitle(for section: Int) -> String?
}

它包括以下默认实现:

  • var numberOfSections: Int
  • func numberOfItems(in section: Int) -> Int
  • func item(at indexPath: IndexPath) -> ItemType?
  • func indexPath(after indexPath: IndexPath) -> IndexPath?
  • func section(at index: Int) -> SectionType?
  • func headerTitle(for section: Int) -> String?
  • func footerTitle(for section: Int) -> String?

示例

class SimpleDataSource: SectionedDataSource {
    typealias ItemType = String
    typealias SectionType = Section<String>

    var sections = [
        Section(items: ["Item 0.0", "Item 0.1", "Item 0.2"]),
        Section(items: ["Item 1.0", "Item 1.1"], headerTitle: "Header 1"),
        Section(items: ["Item 2.0"], headerTitle: "Header 2", footerTitle: "Footer 2")
    ]

    var reloadBlock: ReloadBlock?
}

分区

分区 对象代表一个单独的分区。它包括一个 项目类型 项的数组,以及可选的头部或尾部标题。如果需要任何其他功能,它是可继承的。

open class Section<ItemType> {
    public var items: [ItemType]
    public var headerTitle: String?
    public var footerTitle: String?

    public init(items: [ItemType], headerTitle: String? = nil, footerTitle: String? = nil) {
        self.items = items
        self.headerTitle = headerTitle
        self.footerTitle = footerTitle
    }
}

获取数据源

获取数据源 继承自 数据源,表示一个由 NSFetchResultsController 支持的列表。

public protocol FetchedDataSource: DataSource {
    associatedtype ItemType: NSFetchRequestResult

    var fetchedResultsController: NSFetchedResultsController<ItemType> { get }
}

它包括以下默认实现:

  • var numberOfSections: Int
  • func numberOfItems(in section: Int) -> Int
  • func item(at indexPath: IndexPath) -> ItemType?
  • func indexPath(after indexPath: IndexPath) -> IndexPath?

要求

数据源需要 Swift 5.0 和 iOS 8.3+

安装

数据源可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中

pod "EZDataSource"

作者

Brad Smith

维护者

ezCater 移动团队,[email protected]

许可

数据源可在MIT许可下获得。有关更多信息,请参阅LICENSE文件。