SwiftyTable 2.0.1

SwiftyTable 2.0.1

Yusaku Nishi 维护。



SwiftyTable

iOS 12.0+ Swift 5.0 Cocoapods Compatible Carthage compatible

纯 Swift 库,用于静态类型表格组件。

要求

  • Xcode 10+
  • Swift 5.0+
  • iOS 12.0+

安装

Swift 包管理器

要在 SwiftPM 项目中使用 SwiftyTable 库,请将以下行添加到您的 Package.swift 文件中的依赖项:

.package(url: "https://github.com/jrsaruo/SwiftyTable", from "2.0.0"),

并将 SwiftyTable 添加为您的目标的依赖项

.target(name: "<target>", dependencies: [
    .product(name: "SwiftyTable", package: "SwiftyTable"),
    // other dependencies
]),

CocoaPods

CocoaPods 是 Cocoa 项目的一个依赖关系管理器。

要将 SwiftyTable 集成到您的 Xcode 项目中并使用 CocoaPods,请在您的 Podfile 中添加以下行,并运行 pod install

pod 'SwiftyTable', git: 'https://github.com/jrsaruo/SwiftyTable.git'

Carthage

Carthage 是一个去中心化的依赖项管理器,可以构建您的依赖项并提供二进制框架。

使用 Carthage 将 SwiftyTable 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。

github "jrsaruo/SwiftyTable"

运行 carthage update 构建 framework,并将构建好的 SwiftyTable.framework 拖入您的 Xcode 项目。

特性

1. 可复用性

无需为 UITableViewCellUITableHeaderFooterViewUICollectionViewCellUICollectionReusableView 定义复用标识符。

class CustomCell: UITableViewCell {
    var customProperty: String?
}

let tableView = UITableView()
tableView.register(CustomCell.self) // NO NEED to define reuse identifier!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(of: CustomCell.self, for: indexPath)
    cell.customProperty = "NO NEED to use 'as! CustomCell'"
    return cell
}

2. 静态类型化的表格组件

以下实现难以阅读,并且容易引起错误。

switch indexPath.section {
case 0:
    switch indexPath.row {
    case 0:
        cell.textLabel?.text = "Username"
    case 1:
        cell.textLabel?.text = "Email"
    case 2:
        cell.textLabel?.text = "phoneNumber"
    default:
        break
    }
case 1:
    cell.accessoryType = .disclosureIndicator
default:
    break
}

↓ 如果您使用 SwiftyTable,代码将变得更好!

switch Section(indexPath.section) {
case .profile:
    cell.textLabel?.text = ProfileRow(indexPath.row).title
case .information:
    cell.accessoryType = .disclosureIndicator
}

易于定义表格部分或行

enum Section: Int, TableSection, CaseIterable {
    case profile
    case information
}

enum ProfileRow: Int, TableRow, CaseIterable {
    case username
    case email
    case phoneNumber

    var title: String { ... }
}

enum InformationRow: Int, TableRow, CaseIterable {
    case help
    case aboutApp
}

可读性和灵活性

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return Section.count // automatically returns 2.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch Section(section) {
        case .profile:
            return ProfileRow.count // automatically returns 3.
        case .information:
            return InformationRow.count // automatically returns 2.
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(of: UITableViewCell.self, for: indexPath)

        switch Section(indexPath.section) { // default case is unnecessary.
        case .profile:
            cell.textLabel?.text = ProfileRow(indexPath.row).title
        case .information:
            cell.accessoryType = .disclosureIndicator
        }

        return cell
    }

}

您可以通过这种方式轻松地更改章节或行。即使更改导致一些问题,编译器也会通知您。

enum InformationRow: Int, TableRow, CaseIterable {
    case contact // just add cases to increase rows
    case aboutApp // just swap cases to change the order of rows
    case help
}

用法

请创建具有整数原始值的Section/Row枚举,并符合TableSection/TableRowCaseIterable。**原始值必须与其章节/行号对应**。

enum SomeSection: Int, TableSection, CaseIterable {
    case zero
    case one
    case two
}

SomeSection.count // automatically returns 3.
SomeSection(0) // .zero
SomeSection(1) // .one
SomeSection(2) // .two
SomeSection(3) // assertionFailure

enum SomeRow: Int, TableRow, CaseIterable {
    case zero, one, two, three
}

如果类型不符合CaseIterable,则需要实现count属性。但是,像这样硬编码可能导致错误,因此您应使用CaseIterable

enum SomeSection: Int, TableSection {
    case zero
    case one
    case two
    static var count: Int { return 3 }
}

使用指定的章节/行号初始化章节或行。

let section = SomeSection(indexPath.section)
let row = SomeRow(indexPath.row)

使用指定的章节/行初始化IndexPath。

// these indexPaths are the same.
let indexPath1 = IndexPath(row: 1, section: 0)
let indexPath2 = IndexPath(row: SomeRow.one, section: 0)
let indexPath3 = IndexPath(row: 1, section: SomeSection.zero)
let indexPath4 = IndexPath(row: SomeRow.one, section: SomeSection.zero)

许可证

SwiftyTable处于MIT许可证之下。有关更多信息,请参阅LICENSE文件。