要运行示例项目,请克隆此仓库,然后首先从 Example 目录运行 pod install
。
TableViewContent 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod 'TableViewContent'
您可以如下声明 table view 的 section 和 cell
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")
}
要处理 cell 的选择,请调用 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, akira.matsuda@me.com
TableViewContent 基于 MIT 许可证可用。有关更多信息,请参见 LICENSE 文件。