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