Skip to content

Instantly share code, notes, and snippets.

@elefantel
Created May 2, 2017 16:18
Show Gist options
  • Save elefantel/aeddf8d79a22dcc4046f782dea2a966d to your computer and use it in GitHub Desktop.
Save elefantel/aeddf8d79a22dcc4046f782dea2a966d to your computer and use it in GitHub Desktop.
A custom cell with a title label, a text field and an error label
class CustomCell: UITableViewCell {
var textField: UITextField!
var titleLabel: UILabel!
var errorLabel: UILabel!
static var identifier: String {
return "\(CustomCell.self)"
}
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
self.selectionStyle = .none
errorLabel = UILabel()
errorLabel.textColor = .red
errorLabel.text = "Error message goes here."
errorLabel.font = UIFont.systemFont(ofSize: 10)
textField = UITextField()
textField.font = UIFont.systemFont(ofSize: 16)
textField.borderStyle = .roundedRect
titleLabel = UILabel()
titleLabel.font = UIFont.boldSystemFont(ofSize: 14)
titleLabel.textColor = UIColor(red: 2/255, green: 145/255, blue: 121/255, alpha: 1.0)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
textField.translatesAutoresizingMaskIntoConstraints = false
errorLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(titleLabel)
contentView.addSubview(textField)
contentView.addSubview(errorLabel)
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-18-[titleLabel(80)]-[textField]-18-|", options: [], metrics: nil, views: ["titleLabel": titleLabel, "textField": textField]))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-18-[errorLabel]-18-|", options: [], metrics: nil, views: ["errorLabel": errorLabel]))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[titleLabel(30)][errorLabel(16)]|", options: [], metrics: nil, views: ["titleLabel": titleLabel, "errorLabel": errorLabel]))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[textField(30)][errorLabel(16)]|", options: [], metrics: nil, views: ["textField": textField, "errorLabel": errorLabel]))
}
public func configure(title: String) {
titleLabel.text = title.appending(":")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment