Last active
July 17, 2022 14:07
-
-
Save IanKeen/da02a8005845367bd524665d64fb3984 to your computer and use it in GitHub Desktop.
A simple generic tableview cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| /// A UITableViewCell that loads a custom UIView into its .contentView | |
| /// the typed view can be accessed via the .customView property | |
| open class GenericTableViewCell<View: UIView>: UITableViewCell { | |
| // MARK: - Properties | |
| public let customView = View() | |
| // MARK: - Lifecycle | |
| public override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
| super.init(style: style, reuseIdentifier: reuseIdentifier) | |
| configure() | |
| } | |
| public required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| configure() | |
| } | |
| // MARK: - Private Functions | |
| private func configure() { | |
| contentView.addSubview(customView) | |
| customView.preservesSuperviewLayoutMargins = false | |
| customView.translatesAutoresizingMaskIntoConstraints = false | |
| NSLayoutConstraint.activate([ | |
| customView.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor), | |
| customView.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor), | |
| customView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), | |
| customView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), | |
| ] | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment