平板电脑 0.5.0

平板电脑 0.5.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2016年5月
SPM支持SPM

Max Sokolov维护。



  • 作者
  • Max Sokolov

Alamofire: Elegant Networking in Swift

Tablet.swift

Tablet是一个非常轻量但功能强大的通用地库,它在Swift环境中处理UITableView的dataSource和delegate方法。Tablet的目标是提供一个创建复杂表格视图的最简单方式。使用Tablet,在处理不同部分中的不同cell时,您无需编写混乱的switchif语句。

功能

  • [x] 基于 泛型的类型安全的细胞
  • [x] 将您的模式或视图模型映射到细胞的简单方式
  • [x] 正确处理具有多行标签的自动布局细胞
  • [x] 可链式单元格操作(选择/取消选择等)
  • [x] 支持从代码、xib或Storyboard创建的细胞
  • [x] 自动xib/classes注册
  • [x] 无需子类化
  • [x] 可扩展性
  • [x] 测试

这几乎是构建一个部分中几组细胞所需的所有内容

let builder = TableRowBuilder<String, MyTableViewCell>(items: ["1", "2", "3", "4", "5"])

Tablet依赖于自适应表格视图单元格,尊重cell的可重用性特性,并且以性能为出发点。您无需担心任何问题,只需创建您的cell,设置自动布局约束,并享受其中的乐趣。参见使用部分以了解更多信息。

要求

  • iOS 8.0+
  • Xcode 7.0+
  • Swift 2.2

安装

使用

类型安全可配置细胞

假设您想将cell配置逻辑放入cell本身。假设您想将视图模型传递给cell。您可以使用TableRowBuilder轻松完成此操作。您的cell应遵循ConfigurableCell协议,如下面的示例中所示

import Tablet

class MyTableViewCell : UITableViewCell, ConfigurableCell {

    typealias T = User

    // this method is not required to be implemented if your cell's id equals to class name
    static func reusableIdentifier() -> String {
        return "reusable_id"
    }

    static func estimatedHeight() -> Float {
        return 255
    }

    func configure(item: T) { // item is user here

        textLabel?.text = item.username
        detailTextLabel?.text = item.isActive ? "Active" : "Inactive"
    }
}

一旦实现了该协议,就可以简单地使用TableRowBuilder构建单元格

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>()
rowBuilder += users

director = TableDirector(tableView: tableView)
tableDirector += TableSectionBuilder(rows: [rowBuilder])

非常基本的表格视图

您可能想设置一个非常基础的表格视图,没有任何自定义单元格。在这种情况下,只需使用TableBaseRowBuilder

import Tablet

let rowBuilder = TableBaseRowBuilder<User, UITableViewCell>(items: [user1, user2, user3], id: "reusable_id")
    .action(.configure) { (data) in

        data.cell?.textLabel?.text = data.item.username
        data.cell?.detailTextLabel?.text = data.item.isActive ? "Active" : "Inactive"
    }

let sectionBuilder = TableSectionBuilder(headerTitle: "Users", footerTitle: nil, rows: [rowBuilder])

director = TableDirector(tableView: tableView)
director += sectionBuilder

单元格操作

Tablet提供了一种链式方法来处理单元格操作

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: [user1, user2, user3], id: "reusable_id")
    .action(.configure) { (data) in

    }
    .action(.click) { (data) in

    }
    .valueAction(.shouldHighlight) { (data) in

        return false
    }

自定义单元格操作

import Tablet

struct MyCellActions {
    static let ButtonClicked = "ButtonClicked"
}

class MyTableViewCell : UITableViewCell {

    @IBAction func buttonClicked(sender: UIButton) {

        Action(key: MyCellActions.ButtonClicked, sender: self, userInfo: nil).invoke()
    }
}

并使用行构建器接收这些操作

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
    .action(.click) { (data) in

    }
    .action(.willDisplay) { (data) in

    }
    .action(MyCellActions.ButtonClicked) { (data) in

    }

扩展性

如果你发现Tablet没有提供所需的功能,例如你需要UITableViewDelegate的didEndDisplayingCell方法,而这些方法没有现成的,只需为TableDirector提供一个扩展即可

import Tablet

struct MyTableActions {
    static let DidEndDisplayingCell = "DidEndDisplayingCell"
}

extension TableDirector {

    public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        invoke(action: .custom(MyTableActions.DidEndDisplayingCell), cell: cell, indexPath: indexPath)
    }
}

使用行构建器捕获你的操作

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
    .action(MyTableActions.DidEndDisplayingCell) { (data) -> Void in

    }

你还可以调用返回值的操作。

许可协议

Tablet遵循MIT许可协议。查看LICENSE细节。