Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Last active July 17, 2022 14:07
Show Gist options
  • Save IanKeen/da02a8005845367bd524665d64fb3984 to your computer and use it in GitHub Desktop.
Save IanKeen/da02a8005845367bd524665d64fb3984 to your computer and use it in GitHub Desktop.
A simple generic tableview cell
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