Last active
November 4, 2018 04:13
-
-
Save laevandus/9be598cddef84d85f6b5d45fa84a1965 to your computer and use it in GitHub Desktop.
This file contains 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
final class TextInputTableViewCell: UITableViewCell { | |
// MARK: Initializing a Cell | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
contentView.addSubview(editableTextField) | |
NSLayoutConstraint.activate([ | |
editableTextField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), | |
editableTextField.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), | |
editableTextField.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor) | |
]) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
// MARK: Reusing Cells | |
override func prepareForReuse() { | |
super.prepareForReuse() | |
changeHandler = { _ in } | |
} | |
// MARK: Managing the Content | |
func configure(for model: TextInputFormItem) { | |
editableTextField.text = model.text | |
editableTextField.placeholder = model.placeholder | |
changeHandler = model.didChange | |
} | |
lazy private var editableTextField: UITextField = { | |
let textField = UITextField(frame: .zero) | |
textField.translatesAutoresizingMaskIntoConstraints = false | |
textField.addTarget(self, action: #selector(TextInputTableViewCell.textDidChange), for: .editingChanged) | |
return textField | |
}() | |
// MARK: Handling Text Input | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
editableTextField.becomeFirstResponder() | |
} | |
private var changeHandler: (String) -> () = { _ in } | |
@objc private func textDidChange() { | |
changeHandler(editableTextField.text ?? "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment