Skip to content

Instantly share code, notes, and snippets.

@tooszovski
Created November 12, 2020 14:37
Show Gist options
  • Save tooszovski/2ed8151f6b6b9df36268a4d72d392fac to your computer and use it in GitHub Desktop.
Save tooszovski/2ed8151f6b6b9df36268a4d72d392fac to your computer and use it in GitHub Desktop.
import UIKit
public final class GenericTableCell<View: UIView>: UITableViewCell {
let view = View()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(view)
makeConstraints()
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
preconditionFailure("init(coder:) has not been implemented")
}
}
private extension GenericTableCell {
func makeConstraints() {
let views = ["contentView": contentView,
"genericView": view]
let constraints = [
NSLayoutConstraint.constraints(withVisualFormat: "V:|genericView|",
metrics: nil,
views: views),
NSLayoutConstraint.constraints(withVisualFormat: "H:|genericView|",
metrics: nil,
views: views)
]
.flatMap { $0 }
NSLayoutConstraint.activate(constraints)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment