author | koda |
Thu, 22 Jul 2010 03:08:17 +0200 | |
changeset 3662 | a44406f4369b |
parent 3661 | 2378ada8a6ee |
child 3667 | 9359a70df013 |
permissions | -rw-r--r-- |
3659 | 1 |
// |
2 |
// WeaponCellView.m |
|
3 |
// Hedgewars |
|
4 |
// |
|
5 |
// Created by Vittorio on 03/07/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "EditableCellView.h" |
|
10 |
#import "CommodityFunctions.h" |
|
11 |
||
12 |
@implementation EditableCellView |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
13 |
@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, oldValue; |
3659 | 14 |
|
15 |
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
|
16 |
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
|
17 |
delegate = nil; |
|
18 |
||
19 |
textField = [[UITextField alloc] initWithFrame:CGRectZero]; |
|
20 |
textField.backgroundColor = [UIColor clearColor]; |
|
21 |
textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
|
22 |
textField.delegate = self; |
|
23 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
24 |
textField.clearsOnBeginEditing = NO; |
|
25 |
textField.returnKeyType = UIReturnKeyDone; |
|
26 |
textField.adjustsFontSizeToFitWidth = YES; |
|
27 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
28 |
||
29 |
[self.contentView addSubview:textField]; |
|
30 |
[textField release]; |
|
3660 | 31 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
32 |
titleLabel = [[UILabel alloc] init]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
33 |
titleLabel.textAlignment = UITextAlignmentLeft; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
34 |
titleLabel.backgroundColor = [UIColor clearColor]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
35 |
titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
36 |
[self.contentView addSubview:titleLabel]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
37 |
[titleLabel release]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
38 |
|
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
39 |
minimumCharacters = 1; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
40 |
maximumCharacters = 64; |
3660 | 41 |
oldValue = nil; |
3659 | 42 |
} |
43 |
return self; |
|
44 |
} |
|
45 |
||
46 |
-(void) layoutSubviews { |
|
47 |
[super layoutSubviews]; |
|
48 |
||
49 |
CGRect contentRect = self.contentView.bounds; |
|
50 |
CGFloat boundsX = contentRect.origin.x; |
|
51 |
||
3660 | 52 |
int offset = 0; |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
53 |
int skew = 0; |
3660 | 54 |
if (self.imageView != nil) |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
55 |
offset += self.imageView.frame.size.width; |
3660 | 56 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
57 |
if ([self.titleLabel.text length] == 0) |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
58 |
titleLabel.frame = CGRectZero; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
59 |
else { |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
60 |
titleLabel.frame = CGRectMake(boundsX+offset+10, 10, 100, [UIFont labelFontSize] + 4); |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
61 |
offset += 100; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
62 |
skew +=2; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
63 |
} |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
64 |
|
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
65 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 250, [UIFont labelFontSize] + 4); |
3659 | 66 |
} |
67 |
||
68 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
69 |
[super setSelected:selected animated:animated]; |
|
70 |
// Configure the view for the selected state |
|
71 |
} |
|
72 |
||
73 |
-(void) dealloc { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
74 |
[oldValue release], oldValue = nil; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
75 |
[titleLabel release], titleLabel = nil; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
76 |
[textField release], textField = nil; |
3659 | 77 |
[super dealloc]; |
78 |
} |
|
79 |
||
80 |
#pragma mark - |
|
81 |
#pragma mark textField delegate |
|
82 |
// limit the size of the field to 64 characters like in original frontend |
|
83 |
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
84 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 85 |
} |
86 |
||
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
87 |
// allow editing only if delegate is set and conformant to protocol |
3659 | 88 |
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
89 |
return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)]; |
3659 | 90 |
} |
91 |
||
92 |
// the textfield is being modified, update the navigation controller |
|
93 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 94 |
// don't interact with table below |
3659 | 95 |
((UITableView*)[self superview]).scrollEnabled = NO; |
96 |
||
3660 | 97 |
self.oldValue = self.textField.text; |
98 |
||
3659 | 99 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
100 |
style:UIBarButtonItemStylePlain |
|
101 |
target:self |
|
102 |
action:@selector(cancel:)]; |
|
103 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
104 |
[cancelButton release]; |
|
105 |
||
106 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
|
107 |
style:UIBarButtonItemStyleDone |
|
108 |
target:self |
|
109 |
action:@selector(save:)]; |
|
110 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
111 |
[saveButton release]; |
|
112 |
} |
|
113 |
||
3660 | 114 |
/* with this a field might remain in editing status even if the view moved; |
115 |
use method below instead that allows some more interaction |
|
3659 | 116 |
// don't accept 0-length strings |
117 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
118 |
return ([aTextField.text length] > 0); |
|
119 |
} |
|
3660 | 120 |
*/ |
121 |
||
122 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
123 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 124 |
} |
3659 | 125 |
|
126 |
// the textfield has been modified, tell the delegate to do something |
|
127 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
|
128 |
((UITableView*)[self superview]).scrollEnabled = YES; |
|
129 |
||
130 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
131 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
132 |
} |
|
133 |
||
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
134 |
#pragma mark - |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
135 |
#pragma mark instance methods |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
136 |
// the user wants to show the keyboard |
3659 | 137 |
-(void) replyKeyboard { |
138 |
[self.textField becomeFirstResponder]; |
|
139 |
} |
|
140 |
||
141 |
// the user pressed cancel so hide keyboard |
|
142 |
-(void) cancel:(id) sender { |
|
3660 | 143 |
self.textField.text = self.oldValue; |
144 |
[self save:sender]; |
|
3659 | 145 |
} |
146 |
||
147 |
// send the value to the delegate |
|
148 |
-(void) save:(id) sender { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
149 |
if (delegate == nil || ![delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)]) |
3659 | 150 |
return; |
151 |
||
3660 | 152 |
// don't save if the textfield is invalid |
153 |
if (![self textFieldShouldReturn:textField]) |
|
154 |
return; |
|
155 |
||
156 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
|
3659 | 157 |
[self.textField resignFirstResponder]; |
3660 | 158 |
self.oldValue = nil; |
3659 | 159 |
} |
160 |
||
161 |
@end |