TableViewContent 5.0.1

TableViewContent 5.0.1

Akira Matsuda 维护。



  • 作者:Akira Matsuda
  • Akira Matsuda

TableViewContent

Version License Platform

示例

要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install

要求

安装

TableViewContent 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:

pod 'TableViewContent'

用法

您可以按以下方式声明表格视图的单元格和区段:

Section {
    DefaultRow(title: "title")
    DefaultRow(title: "title", style: .subtitle)
        .detailText("subtitle")
    DefaultRow(title: "title", style: .value1)
        .detailText("value1")
    DefaultRow(title: "title", style: .value2)
        .accessoryType(.disclosureIndicator)
        .detailText("value2")
}

为了处理单元格选择,调用 didSelect 方法。

DefaultRow(title: "title", style: .value2)
.accessoryType(.disclosureIndicator)
.detailText("value2")
.didSelect { _, _, _ in
    let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController")
    self.navigationController?.pushViewController(viewController, animated: true)
}

定义继承自 Row<T: UITableViewCell> 的类,以实现自定义行。

class CustomTableViewCell: UITableViewCell {
    public typealias Action = () -> Void

    @IBOutlet private var button: UIButton!
    var buttonPressedAction: Action = {}

    override func awakeFromNib() {
        super.awakeFromNib()
        button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
    }

    @objc
    private func buttonPressed(_: UIButton) {
        buttonPressedAction()
    }
}

class CustomRow: Row<CustomTableViewCell> {
    public typealias Action = () -> Void

    private var buttonPressedAction: Action = {}

    init() {
        super.init(
            .nib(.init(nibName: "CustomTableViewCell", bundle: nil)),
            reuseIdentifier: NSStringFromClass(CustomTableViewCell.self)
        )
        selectionStyle(.none)
    }

    override func defaultCellConfiguration(_ cell: CustomTableViewCell, _ indexPath: IndexPath) {
        cell.buttonPressedAction = buttonPressedAction
    }

    convenience init(_ action: @escaping Action) {
        self.init()
        buttonPressedAction = action
    }

    @discardableResult
    func didButtonPress(_ action: @escaping Action) -> Self {
        buttonPressedAction = action
        return self
    }

    @objc
    private func buttonPressed() {
        buttonPressedAction()
    }
}

查看示例代码,以了解更高级的使用技巧。

作者

Akira Matsuda, [email protected]

许可证

TableViewContent许可协议为MIT许可。更多信息请参阅LICENSE文件。