author | koda |
Sat, 31 Jul 2010 11:24:53 +0200 | |
changeset 3697 | d5b30d6373fc |
parent 3667 | 9359a70df013 |
child 3782 | dc3531e49e4c |
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; |
|
3697 | 18 |
|
3659 | 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]; |
|
3697 | 28 |
|
3659 | 29 |
[self.contentView addSubview:textField]; |
3667 | 30 |
//[textField release]; |
3697 | 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]; |
3667 | 37 |
//[titleLabel release]; |
3697 | 38 |
|
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
|
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; |
|
3697 | 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; |
3697 | 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 { |
|
3667 | 74 |
self.delegate = nil; |
75 |
releaseAndNil(oldValue); |
|
76 |
releaseAndNil(titleLabel); |
|
77 |
releaseAndNil(textField); |
|
3659 | 78 |
[super dealloc]; |
79 |
} |
|
80 |
||
81 |
#pragma mark - |
|
82 |
#pragma mark textField delegate |
|
83 |
// limit the size of the field to 64 characters like in original frontend |
|
84 |
-(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
|
85 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 86 |
} |
87 |
||
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
|
88 |
// allow editing only if delegate is set and conformant to protocol |
3659 | 89 |
-(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
|
90 |
return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)]; |
3659 | 91 |
} |
92 |
||
93 |
// the textfield is being modified, update the navigation controller |
|
94 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 95 |
// don't interact with table below |
3659 | 96 |
((UITableView*)[self superview]).scrollEnabled = NO; |
3697 | 97 |
|
3660 | 98 |
self.oldValue = self.textField.text; |
3697 | 99 |
|
3659 | 100 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
101 |
style:UIBarButtonItemStylePlain |
|
102 |
target:self |
|
103 |
action:@selector(cancel:)]; |
|
104 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
105 |
[cancelButton release]; |
|
3697 | 106 |
|
3659 | 107 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
108 |
style:UIBarButtonItemStyleDone |
|
109 |
target:self |
|
110 |
action:@selector(save:)]; |
|
111 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
112 |
[saveButton release]; |
|
113 |
} |
|
114 |
||
3660 | 115 |
/* with this a field might remain in editing status even if the view moved; |
116 |
use method below instead that allows some more interaction |
|
3659 | 117 |
// don't accept 0-length strings |
118 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
3697 | 119 |
return ([aTextField.text length] > 0); |
3659 | 120 |
} |
3660 | 121 |
*/ |
122 |
||
123 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3697 | 124 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 125 |
} |
3659 | 126 |
|
127 |
// the textfield has been modified, tell the delegate to do something |
|
128 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
|
129 |
((UITableView*)[self superview]).scrollEnabled = YES; |
|
3697 | 130 |
|
3659 | 131 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
132 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
133 |
} |
|
134 |
||
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
|
135 |
#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
|
136 |
#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
|
137 |
// the user wants to show the keyboard |
3659 | 138 |
-(void) replyKeyboard { |
139 |
[self.textField becomeFirstResponder]; |
|
140 |
} |
|
141 |
||
142 |
// the user pressed cancel so hide keyboard |
|
143 |
-(void) cancel:(id) sender { |
|
3660 | 144 |
self.textField.text = self.oldValue; |
145 |
[self save:sender]; |
|
3659 | 146 |
} |
147 |
||
148 |
// send the value to the delegate |
|
149 |
-(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
|
150 |
if (delegate == nil || ![delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)]) |
3659 | 151 |
return; |
3697 | 152 |
|
3660 | 153 |
// don't save if the textfield is invalid |
154 |
if (![self textFieldShouldReturn:textField]) |
|
155 |
return; |
|
3697 | 156 |
|
3660 | 157 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 158 |
[self.textField resignFirstResponder]; |
3660 | 159 |
self.oldValue = nil; |
3659 | 160 |
} |
161 |
||
162 |
@end |