2012-01-20 38 views
1

我的UITableViewCellUITextField里面,现在我想要得到UITextField数据要存储在数据库(使用插入命令),但我怎么能够访问UITableViewCellUITextField数据tableviewcell有textfield现在我想获得textfileld数据存储在数据库中

@interface ELCTextfieldCell : UITableViewCell <UITextFieldDelegate> { 

    id delegate; 
    UILabel *leftLabel; 
    UITextField *rightTextField; 
    NSIndexPath *indexPath; 
} 

@implementation ELCTextfieldCell 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 

     leftLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [leftLabel setBackgroundColor:[UIColor clearColor]]; 
     [leftLabel setTextColor:[UIColor colorWithRed:.285 green:.376 blue:.541 alpha:1]]; 
     [leftLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
     [leftLabel setTextAlignment:UITextAlignmentRight]; 
     [leftLabel setText:@"Left Field"]; 
     [self addSubview:leftLabel]; 

     rightTextField = [[UITextField alloc] initWithFrame:CGRectZero]; 
     rightTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
     [rightTextField setDelegate:self]; 
     [rightTextField setPlaceholder:@"Right Field"]; 
     [rightTextField setFont:[UIFont systemFontOfSize:17]]; 

     // FOR MWF USE DONE 
     [rightTextField setReturnKeyType:UIReturnKeyDone]; 

     [self addSubview:rightTextField]; 
    } 

    return self; 
} 

//Layout our fields in case of a layoutchange (fix for iPad doing strange things with margins if width is > 400) 
- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect origFrame = self.contentView.frame; 
    if (leftLabel.text != nil) { 
     leftLabel.frame = CGRectMake(origFrame.origin.x, origFrame.origin.y, 90, origFrame.size.height-1); 
     rightTextField.frame = CGRectMake(origFrame.origin.x+105, origFrame.origin.y, origFrame.size.width-120, origFrame.size.height-1); 
    } else { 
     leftLabel.hidden = YES; 
     NSInteger imageWidth = 0; 
     if (self.imageView.image != nil) { 
      imageWidth = self.imageView.image.size.width + 5; 
     } 
     rightTextField.frame = CGRectMake(origFrame.origin.x+imageWidth+10, origFrame.origin.y, origFrame.size.width-imageWidth-20, origFrame.size.height-1); 
    } 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 

    if([delegate respondsToSelector:@selector(textFieldDidReturnWithIndexPath:)]) { 

     [delegate performSelector:@selector(textFieldDidReturnWithIndexPath:) withObject:indexPath]; 
    } 

    return YES; 
} 

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 

    NSString *textString = self.rightTextField.text; 

    if (range.length > 0) { 

     textString = [textString stringByReplacingCharactersInRange:range withString:@""]; 
    } 

    else { 

     if(range.location == [textString length]) { 

      textString = [textString stringByAppendingString:string]; 
     } 

     else { 

      textString = [textString stringByReplacingCharactersInRange:range withString:string]; 
     } 
    } 

    if([delegate respondsToSelector:@selector(updateTextLabelAtIndexPath:string:)]) {  
     [delegate performSelector:@selector(updateTextLabelAtIndexPath:string:) withObject:indexPath withObject:textString]; 
    } 

    return YES; 
} 

@interface RootViewController : UITableViewController <ELCTextFieldDelegate> { 

    NSArray *labels; 
    NSArray *placeholders; 
} 

@implementation RootViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.labels = [NSArray arrayWithObjects:@"First Name", 
              @"Last Name", 
              @"Email", 
              @"Phone Number", 
              nil]; 

    self.placeholders = [NSArray arrayWithObjects:@"Enter First Name", 
                @"Enter Last Name", 
                @"Enter Email", 
                @"Phone Number (Optional)", 
                nil]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    ELCTextfieldCell *cell = (ELCTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[ELCTextfieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 
+0

用于存储数据时,你使用的是什么? sqlite或核心数据? –

回答

1

try标记值设置为文本字段,然后检索值。

例:rightTextField.tag = 1000; //不要使用0

访问使用文本框,

UITextField *textField = (UITextField *)[self.view viewWithTag:1000]; 
相关问题