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 |
|
3660
|
12 |
#define MAX_STRING_LENGTH 64
|
|
13 |
|
3659
|
14 |
@implementation EditableCellView
|
3660
|
15 |
@synthesize delegate, textField, oldValue;
|
3659
|
16 |
|
|
17 |
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
|
18 |
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
|
|
19 |
delegate = nil;
|
|
20 |
|
|
21 |
textField = [[UITextField alloc] initWithFrame:CGRectZero];
|
|
22 |
textField.backgroundColor = [UIColor clearColor];
|
|
23 |
textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
|
|
24 |
textField.delegate = self;
|
|
25 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
26 |
textField.clearsOnBeginEditing = NO;
|
|
27 |
textField.returnKeyType = UIReturnKeyDone;
|
|
28 |
textField.adjustsFontSizeToFitWidth = YES;
|
|
29 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
|
|
30 |
|
|
31 |
[self.contentView addSubview:textField];
|
|
32 |
[textField release];
|
3660
|
33 |
|
|
34 |
oldValue = nil;
|
3659
|
35 |
}
|
|
36 |
return self;
|
|
37 |
}
|
|
38 |
|
|
39 |
-(void) layoutSubviews {
|
|
40 |
[super layoutSubviews];
|
|
41 |
|
|
42 |
CGRect contentRect = self.contentView.bounds;
|
|
43 |
CGFloat boundsX = contentRect.origin.x;
|
|
44 |
|
3660
|
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);
|
3659
|
50 |
}
|
|
51 |
|
|
52 |
/*
|
|
53 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
|
|
54 |
[super setSelected:selected animated:animated];
|
|
55 |
// Configure the view for the selected state
|
|
56 |
}
|
|
57 |
*/
|
|
58 |
|
|
59 |
-(void) dealloc {
|
3660
|
60 |
[oldValue release];
|
3659
|
61 |
[textField release];
|
|
62 |
[super dealloc];
|
|
63 |
}
|
|
64 |
|
|
65 |
#pragma mark -
|
|
66 |
#pragma mark textField delegate
|
|
67 |
// limit the size of the field to 64 characters like in original frontend
|
|
68 |
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
|
3660
|
69 |
return !([aTextField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
|
3659
|
70 |
}
|
|
71 |
|
|
72 |
// allow editing only if delegate is set
|
|
73 |
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
|
|
74 |
return (delegate != nil);
|
|
75 |
}
|
|
76 |
|
|
77 |
// the textfield is being modified, update the navigation controller
|
|
78 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
|
3660
|
79 |
// don't interact with table below
|
3659
|
80 |
((UITableView*)[self superview]).scrollEnabled = NO;
|
|
81 |
|
3660
|
82 |
self.oldValue = self.textField.text;
|
|
83 |
|
3659
|
84 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"")
|
|
85 |
style:UIBarButtonItemStylePlain
|
|
86 |
target:self
|
|
87 |
action:@selector(cancel:)];
|
|
88 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton;
|
|
89 |
[cancelButton release];
|
|
90 |
|
|
91 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"")
|
|
92 |
style:UIBarButtonItemStyleDone
|
|
93 |
target:self
|
|
94 |
action:@selector(save:)];
|
|
95 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton;
|
|
96 |
[saveButton release];
|
|
97 |
}
|
|
98 |
|
3660
|
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
|
3659
|
101 |
// don't accept 0-length strings
|
|
102 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField {
|
|
103 |
return ([aTextField.text length] > 0);
|
|
104 |
}
|
3660
|
105 |
*/
|
|
106 |
|
|
107 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField {
|
|
108 |
return ([aTextField.text length] > 0);
|
|
109 |
}
|
3659
|
110 |
|
|
111 |
// the textfield has been modified, tell the delegate to do something
|
|
112 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
|
|
113 |
((UITableView*)[self superview]).scrollEnabled = YES;
|
|
114 |
|
|
115 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
|
|
116 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
|
|
117 |
}
|
|
118 |
|
|
119 |
-(void) replyKeyboard {
|
|
120 |
[self.textField becomeFirstResponder];
|
|
121 |
}
|
|
122 |
|
|
123 |
// the user pressed cancel so hide keyboard
|
|
124 |
-(void) cancel:(id) sender {
|
3660
|
125 |
self.textField.text = self.oldValue;
|
|
126 |
[self save:sender];
|
3659
|
127 |
}
|
|
128 |
|
|
129 |
// send the value to the delegate
|
|
130 |
-(void) save:(id) sender {
|
|
131 |
if (delegate == nil)
|
|
132 |
return;
|
|
133 |
|
3660
|
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];
|
3659
|
139 |
[self.textField resignFirstResponder];
|
3660
|
140 |
self.oldValue = nil;
|
3659
|
141 |
}
|
|
142 |
|
|
143 |
@end
|