@@ -5,8 +5,21 @@ import UIKit
55
66class EditableTextCell : UITableViewCell {
77 var message : String {
8- get { return valueTextField. text ?? " " }
9- set ( value) { valueTextField. text = value }
8+ get {
9+ return valueTextField. text ?? " "
10+ }
11+ set {
12+ valueTextField. text = newValue
13+ }
14+ }
15+
16+ var placeholder : String ? {
17+ get {
18+ return valueTextField. placeholder
19+ }
20+ set {
21+ valueTextField. placeholder = newValue
22+ }
1023 }
1124
1225 let valueTextField : UITextField = {
@@ -21,22 +34,26 @@ class EditableTextCell: UITableViewCell {
2134 return valueTextField
2235 } ( )
2336
24- var onValueBeingEdited : ( ( String ) -> Void ) ?
37+ var onValueBeingEdited : ( ( EditableTextCell , String ) -> Void ) ?
2538
2639 override init ( style: UITableViewCell . CellStyle , reuseIdentifier: String ? ) {
2740 super. init ( style: style, reuseIdentifier: reuseIdentifier)
2841
29- valueTextField. delegate = self
3042 contentView. addSubview ( valueTextField)
3143 valueTextField. translatesAutoresizingMaskIntoConstraints = false
32- let bottomAnchorConstraint = contentView. layoutMarginsGuide. bottomAnchor. constraint ( equalToSystemSpacingBelow: valueTextField. bottomAnchor, multiplier: 1 )
44+
45+ // Reduce the bottom margin by 0.5pt to maintain the default cell height (44pt)
46+ let bottomAnchorConstraint = contentView. layoutMarginsGuide. bottomAnchor. constraint ( equalTo: valueTextField. bottomAnchor, constant: - 0.5 )
3347 bottomAnchorConstraint. priority = . defaultLow
48+
3449 NSLayoutConstraint . activate ( [
35- valueTextField. leadingAnchor. constraint ( equalToSystemSpacingAfter : contentView. layoutMarginsGuide. leadingAnchor, multiplier : 1 ) ,
36- contentView. layoutMarginsGuide. trailingAnchor. constraint ( equalToSystemSpacingAfter : valueTextField. trailingAnchor, multiplier : 1 ) ,
37- valueTextField . topAnchor. constraint ( equalToSystemSpacingBelow : contentView . layoutMarginsGuide . topAnchor, multiplier : 1 ) ,
50+ valueTextField. leadingAnchor. constraint ( equalTo : contentView. layoutMarginsGuide. leadingAnchor) ,
51+ contentView. layoutMarginsGuide. trailingAnchor. constraint ( equalTo : valueTextField. trailingAnchor) ,
52+ contentView . layoutMarginsGuide . topAnchor. constraint ( equalTo : valueTextField . topAnchor) ,
3853 bottomAnchorConstraint
3954 ] )
55+
56+ NotificationCenter . default. addObserver ( self , selector: #selector( textFieldDidChangeText ( _: ) ) , name: UITextField . textDidChangeNotification, object: valueTextField)
4057 }
4158
4259 required init ? ( coder aDecoder: NSCoder ) {
@@ -50,15 +67,10 @@ class EditableTextCell: UITableViewCell {
5067 override func prepareForReuse( ) {
5168 super. prepareForReuse ( )
5269 message = " "
70+ placeholder = nil
5371 }
54- }
5572
56- extension EditableTextCell : UITextFieldDelegate {
57- func textField( _ textField: UITextField , shouldChangeCharactersIn range: NSRange , replacementString string: String ) -> Bool {
58- if let onValueBeingEdited = onValueBeingEdited {
59- let modifiedText = ( ( textField. text ?? " " ) as NSString ) . replacingCharacters ( in: range, with: string)
60- onValueBeingEdited ( modifiedText)
61- }
62- return true
73+ @objc private func textFieldDidChangeText( _ notification: Notification ) {
74+ onValueBeingEdited ? ( self , valueTextField. text ?? " " )
6375 }
6476}
0 commit comments