TableViewcell с текстовым полем теперь я хотел получить данные textfileld для хранения в базе данных - PullRequest
1 голос
/ 20 января 2012

My UITableViewCell имеет UITextField внутри, теперь я хочу получить UITextField данные для хранения в базе данных (используя команду вставки), но как я могу получить доступ к UITableViewCell * UITextField данным?

@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;
}

1 Ответ

1 голос
/ 20 января 2012

Попробуйте установить значение тега в textField, а затем получить значения.

Пример: rightTextField.tag = 1000; // не использовать 0

Доступ к textField с помощью,

UITextField *textField = (UITextField *)[self.view viewWithTag:1000];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...