Go to file
Akira MATSUDA 81d32deb9f
Merge pull request #1 from 0x0c/dependabot/bundler/cocoapods-downloader-1.6.3
2023-04-16 17:30:08 +09:00
.swiftpm/xcode/package.xcworkspace update package.swift 2020-11-05 10:05:19 +09:00
Example Update example 2022-03-13 17:53:25 +09:00
Image add image 2020-11-09 17:55:45 +09:00
TableViewContent Add `func append(contentsOf elements: [RowRepresentation])` 2022-03-13 17:52:25 +09:00
.gitignore update gitignore 2019-05-20 22:31:11 +09:00
.swift-version format 2020-11-09 16:49:14 +09:00
.swiftformat Ran SwiftFormat 2020-11-19 16:38:19 +09:00
.travis.yml Initial commit 2018-10-08 17:01:31 +09:00
Gemfile Add Gemfile. 2019-05-20 22:31:18 +09:00
Gemfile.lock Merge pull request #1 from 0x0c/dependabot/bundler/cocoapods-downloader-1.6.3 2023-04-16 17:30:08 +09:00
LICENSE Initial commit 2018-10-08 17:01:31 +09:00
Package.swift Ran SwiftFormat 2020-11-19 16:38:19 +09:00
README.md Update README.md 2020-11-25 07:13:17 +09:00
TableViewContent.podspec Update version 2022-03-13 17:53:39 +09:00
_Pods.xcodeproj Initial commit 2018-10-08 17:01:31 +09:00
run_formatter.sh Ran SwiftFormat 2020-11-19 16:38:19 +09:00

README.md

TableViewContent

Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

TableViewContent is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TableViewContent'

Usage

You can declare table view sections and cells as follows:

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")
}

To handle cell selection, call didSelect method.

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)
}

Define class that inherit Row<T: UITableViewCell> for implementing custom row.

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()
    }
}

See example code to lean advanced usage.

Author

Akira Matsuda, akira.matsuda@me.com

License

TableViewContent is available under the MIT license. See the LICENSE file for more info.