project_files/HedgewarsMobile/Classes/EditableCellView.m
changeset 3662 a44406f4369b
parent 3661 2378ada8a6ee
child 3667 9359a70df013
equal deleted inserted replaced
3661:2378ada8a6ee 3662:a44406f4369b
     8 
     8 
     9 #import "EditableCellView.h"
     9 #import "EditableCellView.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 
    11 
    12 @implementation EditableCellView
    12 @implementation EditableCellView
    13 @synthesize delegate, textField, oldValue;
    13 @synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, oldValue;
    14 
    14 
    15 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    15 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    16     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    16     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    17         delegate = nil;
    17         delegate = nil;
    18         
    18         
    27         [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
    27         [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
    28         
    28         
    29         [self.contentView addSubview:textField];
    29         [self.contentView addSubview:textField];
    30         [textField release];
    30         [textField release];
    31         
    31         
       
    32         titleLabel = [[UILabel alloc] init];
       
    33         titleLabel.textAlignment = UITextAlignmentLeft;
       
    34         titleLabel.backgroundColor = [UIColor clearColor];
       
    35         titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
       
    36         [self.contentView addSubview:titleLabel];
       
    37         [titleLabel release];
       
    38         
       
    39         minimumCharacters = 1;
       
    40         maximumCharacters = 64;
    32         oldValue = nil;
    41         oldValue = nil;
    33     }
    42     }
    34     return self;
    43     return self;
    35 }
    44 }
    36 
    45 
    39 
    48 
    40     CGRect contentRect = self.contentView.bounds;
    49     CGRect contentRect = self.contentView.bounds;
    41     CGFloat boundsX = contentRect.origin.x;
    50     CGFloat boundsX = contentRect.origin.x;
    42     
    51     
    43     int offset = 0;
    52     int offset = 0;
       
    53     int skew = 0;
    44     if (self.imageView != nil)
    54     if (self.imageView != nil)
    45         offset = self.imageView.frame.size.width;
    55         offset += self.imageView.frame.size.width;
    46     
    56     
    47     textField.frame = CGRectMake(boundsX+offset+10, 10, 250, [UIFont labelFontSize] + 4);
    57     if ([self.titleLabel.text length] == 0)
       
    58         titleLabel.frame = CGRectZero;
       
    59     else {
       
    60         titleLabel.frame = CGRectMake(boundsX+offset+10, 10, 100, [UIFont labelFontSize] + 4);
       
    61         offset += 100;
       
    62         skew +=2;
       
    63     }
       
    64 
       
    65     textField.frame = CGRectMake(boundsX+offset+10, skew+10, 250, [UIFont labelFontSize] + 4);
    48 }
    66 }
    49 
    67 
    50 /*
       
    51 -(void) setSelected:(BOOL)selected animated:(BOOL)animated {
    68 -(void) setSelected:(BOOL)selected animated:(BOOL)animated {
    52     [super setSelected:selected animated:animated];
    69     [super setSelected:selected animated:animated];
    53     // Configure the view for the selected state
    70     // Configure the view for the selected state
    54 }
    71 }
    55 */
       
    56 
    72 
    57 -(void) dealloc {
    73 -(void) dealloc {
    58     [oldValue release];
    74     [oldValue release], oldValue = nil;
    59     [textField release];
    75     [titleLabel release], titleLabel = nil;
       
    76     [textField release], textField = nil;
    60     [super dealloc];
    77     [super dealloc];
    61 }
    78 }
    62 
    79 
    63 #pragma mark -
    80 #pragma mark -
    64 #pragma mark textField delegate
    81 #pragma mark textField delegate
    65 // limit the size of the field to 64 characters like in original frontend
    82 // limit the size of the field to 64 characters like in original frontend
    66 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    83 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    67     return !([aTextField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
    84     return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length);
    68 }
    85 }
    69 
    86 
    70 // allow editing only if delegate is set 
    87 // allow editing only if delegate is set and conformant to protocol
    71 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
    88 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
    72     return (delegate != nil);
    89     return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)];
    73 }
    90 }
    74 
    91 
    75 // the textfield is being modified, update the navigation controller
    92 // the textfield is being modified, update the navigation controller
    76 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{
    93 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{
    77     // don't interact with table below
    94     // don't interact with table below
   101     return ([aTextField.text length] > 0); 
   118     return ([aTextField.text length] > 0); 
   102 }
   119 }
   103 */
   120 */
   104 
   121 
   105 -(BOOL) textFieldShouldReturn:(UITextField *)aTextField {
   122 -(BOOL) textFieldShouldReturn:(UITextField *)aTextField {
   106     return ([aTextField.text length] > 0); 
   123     return ([aTextField.text length] >= self.minimumCharacters); 
   107 }
   124 }
   108 
   125 
   109 // the textfield has been modified, tell the delegate to do something
   126 // the textfield has been modified, tell the delegate to do something
   110 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
   127 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
   111     ((UITableView*)[self superview]).scrollEnabled = YES;
   128     ((UITableView*)[self superview]).scrollEnabled = YES;
   112     
   129     
   113     [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
   130     [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
   114     [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
   131     [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
   115 }
   132 }
   116 
   133 
       
   134 #pragma mark -
       
   135 #pragma mark instance methods
       
   136 // the user wants to show the keyboard
   117 -(void) replyKeyboard {
   137 -(void) replyKeyboard {
   118     [self.textField becomeFirstResponder];
   138     [self.textField becomeFirstResponder];
   119 }
   139 }
   120 
   140 
   121 // the user pressed cancel so hide keyboard
   141 // the user pressed cancel so hide keyboard
   124     [self save:sender];
   144     [self save:sender];
   125 }
   145 }
   126 
   146 
   127 // send the value to the delegate
   147 // send the value to the delegate
   128 -(void) save:(id) sender {
   148 -(void) save:(id) sender {
   129     if (delegate == nil)
   149     if (delegate == nil || ![delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)])
   130         return;
   150         return;
   131     
   151     
   132     // don't save if the textfield is invalid
   152     // don't save if the textfield is invalid
   133     if (![self textFieldShouldReturn:textField])
   153     if (![self textFieldShouldReturn:textField])
   134         return;
   154         return;