0

我想在每个 tableview 上添加文本字段,并使用它们的标签访问文本字段的文本。我该怎么做,见下图。

在此处输入图像描述

4

3 回答 3

1

你必须自定义你的 tableViewCell 你可以在你的cellForRowAtIndexPath方法中做到这一点。

当您在该方法中创建单元格时,您可以执行以下操作:

UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 101;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
[textField release];

之后,您想要访问您的文本字段的每个人都可以执行以下操作:

UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];

希望这就是你要找的..

更新

如您所见,我将标签 101 分配为静态。这可以是任何数字,但请确保在同一单元格中没有任何其他具有相同标签的视图。如果您在另一个单元格中,则不是问题。

现在在任何地方,如果让我们说didSelectRowAtIndexPath您可以执行以下操作:

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 UITextField *myTextField = (UITextFiled*)[cell viewWithTag:101];

这样,您将首先获得您的手机。现在,您的单元格只有一个带有 101 标签的视图,因此无论您对其他 textField 是否有相同的标签,您都可以获取您的文本字段,因为该单元格并非对所有人都相同。

干杯

于 2012-05-18T04:22:36.347 回答
1
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
myTextField.tag=indexPath.row;

这种方式分配 10 个文本字段标签。在 textfield 委托方法之后,您可以从不同的 10 个标签中检索该文本。

于 2012-05-18T05:49:01.340 回答
1

// 要从 UITextField 获取文本,请在初始化 Textfield 后添加以下代码

[yourTextField addTarget:self action:@selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];

和这个方法

- (void) getTextFieldValue:(UITextField *)textField{
    NSLog(@"GetTextvalue");
    if (textField.tag == 0) {
        NSLog(@"1--->%@",textField.text);
    }else if(textField.tag == 1){
        NSLog(@"2--->%@",textField.text);
    }else if(textField.tag == 2){
        NSLog(@"3--->%@",textField.text);
    }
}
于 2012-05-18T06:07:32.807 回答