project_files/HedgewarsMobile/Classes/EditableCellView.m
changeset 3904 22e4d74240e5
parent 3878 348f4104461d
child 4208 dd54999c2822
equal deleted inserted replaced
3903:db01c37494af 3904:22e4d74240e5
    21 
    21 
    22 #import "EditableCellView.h"
    22 #import "EditableCellView.h"
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 
    24 
    25 @implementation EditableCellView
    25 @implementation EditableCellView
    26 @synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, oldValue;
    26 @synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, respectEditing, oldValue;
    27 
    27 
    28 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    28 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    29     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    29     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    30         delegate = nil;
    30         delegate = nil;
    31 
    31 
    35         textField.delegate = self;
    35         textField.delegate = self;
    36         textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    36         textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    37         textField.clearsOnBeginEditing = NO;
    37         textField.clearsOnBeginEditing = NO;
    38         textField.returnKeyType = UIReturnKeyDone;
    38         textField.returnKeyType = UIReturnKeyDone;
    39         textField.adjustsFontSizeToFitWidth = YES;
    39         textField.adjustsFontSizeToFitWidth = YES;
       
    40         textField.userInteractionEnabled = YES;
    40         [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
    41         [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
    41 
    42 
    42         [self.contentView addSubview:textField];
    43         [self.contentView addSubview:textField];
    43         //[textField release];
    44         //[textField release];
    44 
    45 
    49         [self.contentView addSubview:titleLabel];
    50         [self.contentView addSubview:titleLabel];
    50         //[titleLabel release];
    51         //[titleLabel release];
    51 
    52 
    52         minimumCharacters = 1;
    53         minimumCharacters = 1;
    53         maximumCharacters = 64;
    54         maximumCharacters = 64;
       
    55         respectEditing = NO;
    54         oldValue = nil;
    56         oldValue = nil;
    55     }
    57     }
    56     return self;
    58     return self;
    57 }
    59 }
    58 
    60 
    96 // limit the size of the field to 64 characters like in original frontend
    98 // limit the size of the field to 64 characters like in original frontend
    97 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    99 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    98     return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length);
   100     return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length);
    99 }
   101 }
   100 
   102 
   101 // allow editing only if delegate is set and conformant to protocol
   103 // allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing
   102 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
   104 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
   103     return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)];
   105     return (delegate != nil) &&
       
   106            [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] &&
       
   107            (respectEditing) ? ((UITableView*)[self superview]).editing : YES;
   104 }
   108 }
   105 
   109 
   106 // the textfield is being modified, update the navigation controller
   110 // the textfield is being modified, update the navigation controller
   107 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{
   111 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{
   108     // don't interact with table below
   112     // don't interact with table below
   175     [delegate saveTextFieldValue:self.textField.text withTag:self.tag];
   179     [delegate saveTextFieldValue:self.textField.text withTag:self.tag];
   176     [self.textField resignFirstResponder];
   180     [self.textField resignFirstResponder];
   177     self.oldValue = nil;
   181     self.oldValue = nil;
   178 }
   182 }
   179 
   183 
       
   184 // when field is editable only when the tableview is editable, resign responder when exiting editing mode
       
   185 -(void) willTransitionToState:(UITableViewCellStateMask)state {
       
   186     if (respectEditing && state == UITableViewCellStateDefaultMask)
       
   187         [self save:nil];
       
   188 
       
   189     [super willTransitionToState:state];
       
   190 }
       
   191 
   180 @end
   192 @end