【问题标题】:How to get the text field values in the Table view in iPhone?如何在 iPhone 的表格视图中获取文本字段值?
【发布时间】:2012-07-04 23:44:38
【问题描述】:

我已经创建了包含用户名和密码的登录页面。我创建了一个自定义单元格,它有一个标签和一个文本字段。我创建了一个分组表视图并使用了自定义单元格。实际上我已经成功创建,但问题是,如何获取用户名和密码的文本字段值。因为我只为这两个字段使用了一个文本字段。我无法正确获取文本字段值。我总是得到两个字段的最后一个值。如何正确获取用户名和传递 word 文本值?

这是我的示例代码,

在自定义单元格中,

@interface CustomCell : UITableViewCell {

    UILabel *userLbl;

    UITextField *userTxt;

}

@property (nonatomic, retain) IBOutlet UILabel *userLbl;

@property (nonatomic, retain) IBOutlet UITextField *userTxt;

@end

在根视图控制器中,

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

        for(id currentObjects in nibObjects)
        {
            if([currentObjects isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *) currentObjects;
            }
        }
    }
    if (indexPath.section == 0) {

        if(indexPath.row == 0)
        {
            [[cell userLbl] setText:@"User Name:"];

            cell.userTxt.tag = 0;

            //self.userName = [[cell userTxt] text];


        }
        if (indexPath.row == 1) {

            [[cell userLbl] setText:@"Pass Word:"];

            cell.userTxt.secureTextEntry = YES;

            //self.passWord = [[cell userTxt] text];

            cell.userTxt.tag = 1;

        }

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    // Configure the cell.

    return cell;
}

-(IBAction) submitAction : (id) sender
{

    self.userName = [[cell userTxt] text];

    self.passWord = [[cell userTxt] text];

    [cell.userTxt resignFirstResponder];

    NSLog(@"The user Name is %@", self.userName);

    NSLog(@"The password is %@", self.passWord);

     //Doesn't work

 /*   UITextField *txtField = (UITextField*)[cell viewWithTag:0];

    self.userName = [txtField text];

    UITextField *txtField1 = (UITextField*)[cell viewWithTag:1];

    self.passWord = [txtField1 text];

  NSLog(@"The user Name is %@",  self.userName);

  NSLog(@"The password is %@", self.passWord);*/

}

这是我的图像屏幕截图,

请帮帮我。

谢谢。

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    如果您使用多个文本字段,则可以使用 textFieldShouldEndEditing 方法,因为 textFieldShouldReturn 方法只会从退出 FirstResponder 的文本字段中获取字符串,因此您将错过所有其他文本字段。这是我在最近的项目中使用的,当时我有一个使用 tableview 和 textfields 构建的表单。让我困惑了一阵子。

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        switch (textField.tag) 
        {
            case 0:
                userName = textField.text;
                break;
    
            case 1:
                passWord = textField.text;
                break;
        }
        return YES;
    }
    

    【讨论】:

      【解决方案2】:

      为每个文本字段和调用使用标签 -

      UITextField *txtField = (UITextField*)[cell viewWithTag:tag];
      NSString *text = [txtField text];
      

      而且 7KV7 是正确的,您将相同的标签分配给两个文本字段。

      【讨论】:

        【解决方案3】:

        为什么不优雅地做呢?在您的 UITextFieldDelegate 方法中,这样做:

        - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        
           switch (textField.tag) {
            case 0:
                self.userName = textField.text;
                break;
            case 1:
                self.passWord = textField.text;
                break;   
            default:
                break;
         }
        }
        

        这样,当用户单击提交时,您已经拥有数据。只是一个想法。

        【讨论】:

          【解决方案4】:

          我认为您为文本字段设置标签的方式存在一些问题。我猜两者都有相同的标签 1. 请看看是否有帮助

          更新

          顺便说一句,您如何访问方法submitAction 中的单元格。我认为将textfields 中的文本分配给一个实例变量比如usernamepassword 并在你的方法中使用它更容易。

          【讨论】:

          • 我无法获得您的最新答案,所以请详细解释一下。谢谢。
          • 顺便说一句,您如何访问方法 submitAction 中的单元格。我认为将文本字段中的文本分配给实例变量(例如用户名和密码)并在您的方法中使用它更容易。
          • 感谢您的快速回复。问题是,我在自定义单元格中创建了一个文本字段,并在表格视图中用于用户名和密码字段。如果我设置标签值,则无法正确设置。 -(IBAction) submitAction : (id) sender { UITextField txtField = (UITextField)[cell viewWithTag:0]; NSString text = [txtField text]; UITextField *txtField1 = (UITextField)[cell viewWithTag:1]; NSString *text1 = [txtField1 文本]; NSLog(@"用户名为%@", text); NSLog(@"密码是%@", text1); }
          • 我不认为你可以在这个方法中得到cell。将文本存储在 ivar 中并在您的方法中使用它
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-15
          • 1970-01-01
          相关资源
          最近更新 更多