开发者

UITableViewCell with dynamically added UITextField using addSubview - How to get UITextField values when 'return' hit on keyboard

开发者 https://www.devze.com 2023-02-16 13:05 出处:网络
I\'ve created a login screen using a UITableView wi开发者_StackOverflow社区th one section and two cells. This is how these cells are created. Now I don\'t know how to retrieve the values from these ce

I've created a login screen using a UITableView wi开发者_StackOverflow社区th one section and two cells. This is how these cells are created. Now I don't know how to retrieve the values from these cells later.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"LoginCellIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
        leftLabel.backgroundColor = [UIColor clearColor];
        leftLabel.tag = 1;
        [cell.contentView addSubview:leftLabel];
        [leftLabel release];

        UITextField *valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 400, 35)];
        valueTextField.tag = 2;
        valueTextField.delegate = self;
        [cell.contentView addSubview:valueTextField];
        [valueTextField release];
    }

    if (indexPath.row == 0) {   // User name
        UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
        lblText.text = @"Username: ";

        UITextField *userNameField = (UITextField *)[cell.contentView viewWithTag:2];
        userNameField.placeholder = @"Enter your username here";        
    }
    else {  // Pass word
        UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
        lblText.text = @"Password: ";

        UITextField *passwordField = (UITextField *)[cell.contentView viewWithTag:2];
        passwordField.placeholder = @"Enter your password here";
        passwordField.secureTextEntry = YES;
    }

    return cell;
}

Precisely, I want to retrieve value when a user hits the return key. So, I want to get the cell's text field values here...

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

    NSLog(@"%@ is the Value", textField.text);
    [textField resignFirstResponder];

    return YES;
}

But, I don't know how to retrieve those text fields' values. I wonder cellForRowAtIndexPath for index path would work in this case or not?


You should use the textFieldDidEndEditing: method as mentioned by @gnuchu but to detect which textfield just finished editing you can use this code:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    NSLog(@"Row %d just finished editing with the value %@",textFieldIndexPath.row,textField.text);
}

The above code should work for you, but using a UITableView for 2 fixed cells is overkill and just adds unncecessary complication to your code. IMO you would be better off just using a standard view with 2 labels and 2 textfields. This could easily be created in Interface Builder or code and would simplify things considerably.


You should implement the UITextFieldDelegate (documentation link here). That way you'll be able to implement the

- (void)textFieldDidEndEditing:(UITextField *)textField

method which will fire when the user finishes editing the textfield.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号