@@ -5,8 +5,21 @@ import UIKit
5
5
6
6
class EditableTextCell : UITableViewCell {
7
7
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
+ }
10
23
}
11
24
12
25
let valueTextField : UITextField = {
@@ -21,22 +34,26 @@ class EditableTextCell: UITableViewCell {
21
34
return valueTextField
22
35
} ( )
23
36
24
- var onValueBeingEdited : ( ( String ) -> Void ) ?
37
+ var onValueBeingEdited : ( ( EditableTextCell , String ) -> Void ) ?
25
38
26
39
override init ( style: UITableViewCell . CellStyle , reuseIdentifier: String ? ) {
27
40
super. init ( style: style, reuseIdentifier: reuseIdentifier)
28
41
29
- valueTextField. delegate = self
30
42
contentView. addSubview ( valueTextField)
31
43
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 )
33
47
bottomAnchorConstraint. priority = . defaultLow
48
+
34
49
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) ,
38
53
bottomAnchorConstraint
39
54
] )
55
+
56
+ NotificationCenter . default. addObserver ( self , selector: #selector( textFieldDidChangeText ( _: ) ) , name: UITextField . textDidChangeNotification, object: valueTextField)
40
57
}
41
58
42
59
required init ? ( coder aDecoder: NSCoder ) {
@@ -50,15 +67,10 @@ class EditableTextCell: UITableViewCell {
50
67
override func prepareForReuse( ) {
51
68
super. prepareForReuse ( )
52
69
message = " "
70
+ placeholder = nil
53
71
}
54
- }
55
72
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 ?? " " )
63
75
}
64
76
}
0 commit comments