使用addSubview动态添加UITextField的UITableViewCell - 当键盘上的'返回'命中时如何获取UITextField值

问题描述:

我已经创建了一个使用带有一个部分和两个单元格的UITableView的登录屏幕。这是如何创建这些单元格的。现在我不知道如何从以后的这些单元格中检索值。使用addSubview动态添加UITextField的UITableViewCell - 当键盘上的'返回'命中时如何获取UITextField值

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

准确地说,我希望当用户点击回报键来检索值。所以,我想在这里获取单元格的文本字段值...

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

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

    return YES; 
} 

但是,我不知道如何检索这些文本字段的值。我想知道索引路径的cellForRowAtIndexPath在这种情况下还是不行?

您应该使用textFieldDidEndEditing:方法,通过@gnuchu提及,但检测哪些文本框只是完成编辑,你可以使用此代码:

- (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); 
} 

上面的代码应该为你工作,但使用一UITableView for 2 fixed cells is overkill and just added uncenaryary complication to your code。国际海事组织你最好只使用2标签和2个文本框的标准视图。这可以很容易地在Interface Builder或代码中创建,并且可以大大简化。

+0

感谢克里斯它适用于一个单元格,但不击中其他单元格(无论哪个文本字段当前拿着键盘)。检查这个代码..记得当用户点击'返回'键时我想获得价值.. – user653662 2011-03-14 13:43:10

+0

明白了Chris ..我将你的代码移到了常用函数中,并在textFieldDidEndEditing和textFieldShouldReturn(在做其他事情之前)中调用它。 – user653662 2011-03-14 14:00:23

+0

+1 @ theChrisKent,很好的回答,早点睡觉,谢谢 – NAZIK 2013-01-31 18:30:37

您应该实现UITextFieldDelegate(文档链接here)。这样你就可以实现

- (void)textFieldDidEndEditing:(UITextField *)textField 

当用户完成编辑文本字段时将触发的方法。