7 // |
7 // |
8 |
8 |
9 #import "EditableCellView.h" |
9 #import "EditableCellView.h" |
10 #import "CommodityFunctions.h" |
10 #import "CommodityFunctions.h" |
11 |
11 |
|
12 #define MAX_STRING_LENGTH 64 |
|
13 |
12 @implementation EditableCellView |
14 @implementation EditableCellView |
13 @synthesize delegate, textField; |
15 @synthesize delegate, textField, oldValue; |
14 |
16 |
15 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
17 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
16 if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
18 if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
17 delegate = nil; |
19 delegate = nil; |
18 |
20 |
26 textField.adjustsFontSizeToFitWidth = YES; |
28 textField.adjustsFontSizeToFitWidth = YES; |
27 [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
29 [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
28 |
30 |
29 [self.contentView addSubview:textField]; |
31 [self.contentView addSubview:textField]; |
30 [textField release]; |
32 [textField release]; |
|
33 |
|
34 oldValue = nil; |
31 } |
35 } |
32 return self; |
36 return self; |
33 } |
37 } |
34 |
38 |
35 -(void) layoutSubviews { |
39 -(void) layoutSubviews { |
36 [super layoutSubviews]; |
40 [super layoutSubviews]; |
37 |
41 |
38 CGRect contentRect = self.contentView.bounds; |
42 CGRect contentRect = self.contentView.bounds; |
39 CGFloat boundsX = contentRect.origin.x; |
43 CGFloat boundsX = contentRect.origin.x; |
40 |
44 |
41 textField.frame = CGRectMake(boundsX+10, 11, 250, [UIFont labelFontSize] + 2); |
45 int offset = 0; |
|
46 if (self.imageView != nil) |
|
47 offset = self.imageView.frame.size.width; |
|
48 |
|
49 textField.frame = CGRectMake(boundsX+offset+10, 10, 250, [UIFont labelFontSize] + 4); |
42 } |
50 } |
43 |
51 |
44 /* |
52 /* |
45 -(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
53 -(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
46 [super setSelected:selected animated:animated]; |
54 [super setSelected:selected animated:animated]; |
47 // Configure the view for the selected state |
55 // Configure the view for the selected state |
48 } |
56 } |
49 */ |
57 */ |
50 |
58 |
51 -(void) dealloc { |
59 -(void) dealloc { |
|
60 [oldValue release]; |
52 [textField release]; |
61 [textField release]; |
53 [super dealloc]; |
62 [super dealloc]; |
54 } |
63 } |
55 |
64 |
56 #pragma mark - |
65 #pragma mark - |
57 #pragma mark textField delegate |
66 #pragma mark textField delegate |
58 // limit the size of the field to 64 characters like in original frontend |
67 // limit the size of the field to 64 characters like in original frontend |
59 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
68 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
60 int limit = 64; |
69 return !([aTextField.text length] > MAX_STRING_LENGTH && [string length] > range.length); |
61 return !([aTextField.text length] > limit && [string length] > range.length); |
|
62 } |
70 } |
63 |
71 |
64 // allow editing only if delegate is set |
72 // allow editing only if delegate is set |
65 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
73 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
66 return (delegate != nil); |
74 return (delegate != nil); |
67 } |
75 } |
68 |
76 |
69 // the textfield is being modified, update the navigation controller |
77 // the textfield is being modified, update the navigation controller |
70 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
78 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
71 // don't scroll when editing |
79 // don't interact with table below |
72 ((UITableView*)[self superview]).scrollEnabled = NO; |
80 ((UITableView*)[self superview]).scrollEnabled = NO; |
|
81 |
|
82 self.oldValue = self.textField.text; |
73 |
83 |
74 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
84 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
75 style:UIBarButtonItemStylePlain |
85 style:UIBarButtonItemStylePlain |
76 target:self |
86 target:self |
77 action:@selector(cancel:)]; |
87 action:@selector(cancel:)]; |
84 action:@selector(save:)]; |
94 action:@selector(save:)]; |
85 [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
95 [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
86 [saveButton release]; |
96 [saveButton release]; |
87 } |
97 } |
88 |
98 |
89 |
99 /* with this a field might remain in editing status even if the view moved; |
|
100 use method below instead that allows some more interaction |
90 // don't accept 0-length strings |
101 // don't accept 0-length strings |
91 -(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
102 -(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
103 return ([aTextField.text length] > 0); |
|
104 } |
|
105 */ |
|
106 |
|
107 -(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
92 return ([aTextField.text length] > 0); |
108 return ([aTextField.text length] > 0); |
93 } |
109 } |
94 |
110 |
95 // the textfield has been modified, tell the delegate to do something |
111 // the textfield has been modified, tell the delegate to do something |
96 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
112 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
104 [self.textField becomeFirstResponder]; |
120 [self.textField becomeFirstResponder]; |
105 } |
121 } |
106 |
122 |
107 // the user pressed cancel so hide keyboard |
123 // the user pressed cancel so hide keyboard |
108 -(void) cancel:(id) sender { |
124 -(void) cancel:(id) sender { |
109 [self.textField resignFirstResponder]; |
125 self.textField.text = self.oldValue; |
|
126 [self save:sender]; |
110 } |
127 } |
111 |
128 |
112 // send the value to the delegate |
129 // send the value to the delegate |
113 -(void) save:(id) sender { |
130 -(void) save:(id) sender { |
114 if (delegate == nil) |
131 if (delegate == nil) |
115 return; |
132 return; |
116 |
133 |
117 [delegate saveTextFieldValue:self.textField.text]; |
134 // don't save if the textfield is invalid |
|
135 if (![self textFieldShouldReturn:textField]) |
|
136 return; |
|
137 |
|
138 [delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
118 [self.textField resignFirstResponder]; |
139 [self.textField resignFirstResponder]; |
|
140 self.oldValue = nil; |
119 } |
141 } |
120 |
142 |
121 @end |
143 @end |