Skip to content

Instantly share code, notes, and snippets.

@iThinker
Created May 10, 2017 19:24
Show Gist options
  • Save iThinker/0adbfd1391c455c7a8c6fa11aa25e36b to your computer and use it in GitHub Desktop.
Save iThinker/0adbfd1391c455c7a8c6fa11aa25e36b to your computer and use it in GitHub Desktop.
import UIKit
class TableViewContainerCell<Content: UIView>: UITableViewCell {
var content: Content
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
if let viewFromNib = Content.loadDefaultNib() {
content = viewFromNib
}
else {
content = Content(frame: .zero)
}
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.embedContent()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func embedContent() {
let containerView = self.contentView
containerView.addSubview(self.content)
self.content.translatesAutoresizingMaskIntoConstraints = false
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view": self.content]))
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]->=0-|", options: [], metrics: nil, views: ["view": self.content]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment