0

再会!

我有一个UITableView两个单元格及其对应的UITextField. 我通过for 循环创建了UITextField

- (UITextField *)createInformationTextField:(NSString *)createInformationTextField createInformationPlaceholder:(NSString *)createInformationPlaceholder
{
    for (int i = 0; i < 2; i++) {
        informationTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 170, 30)];

        informationTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        informationTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        informationTextField.placeholder = createInformationPlaceholder;
        informationTextField.text = createInformationTextField;
        informationTextField.adjustsFontSizeToFitWidth = YES;
        informationTextField.clearButtonMode = UITextFieldViewModeAlways;

        informationTextField.tag = i;
    }

    return informationTextField;
}

我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *name;
    NSString *address;

    switch (indexPath.section) {
        case 0:
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            switch (indexPath.row) {
                case 0:
                    informationTextField = [self createInformationTextField:name createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];

                    cell.textLabel.text = @"Name";
                    [cell addSubview:informationTextField];
                    break;

                case 1:
                    informationTextField = [self createInformationTextField:address createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];

                    cell.textLabel.text = @"Address";
                    [cell addSubview:informationTextField];
                    break;

                default:
                    break;
            }

            break;

        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"State";
                    break;

                default:
                    break;
            }

            break;

        default:
            break;
    }

    informationTextField.delegate = self;

    return cell;
}

我将UITableView 占位符设置为显示其相应的标签,并且工作正常。

截屏:

在此处输入图像描述

问题出在我的textFieldDidEndEditing

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            NSLog(@"Name Field. The tag is %d", textField.tag);
            break;

        case 1:
            NSLog(@"Address Field. The tag is %d", textField.tag);
            break;

        default:
            break;
    }
}

在用户在 中输入内容,我尝试再次输出其标签,但这就是我所看到的UITextField

2013-06-06 17:36:53.050 UITableUITextField[20387:c07] Address Field. The tag is 1
2013-06-06 17:36:55.636 UITableUITextField[20387:c07] Address Field. The tag is 1

苹果的文档

4

2 回答 2

1

您正在调用createInformationTextField方法来创建文本字段。我认为您正在从每个单元格调用此方法。因此,当您调用此方法时,它会进入 for 循环,并且在两次循环之后,它会返回带有标签的文本字段 1 为每个单元格返回。这就是为什么您为每个文本字段获得标签 1 的原因。尝试从您的cellForRowAtIndexPath:方法调用以下方法

 - (UITextField *)createInformationTextField:(NSString *)createInformationTextField createInformationPlaceholder:(NSString *)createInformationPlaceholder index:(NSIndexPath*)indexPath
    {

        UITextField *informationTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 170, 30)];

        informationTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        informationTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        informationTextField.placeholder = createInformationPlaceholder;
        informationTextField.text = createInformationTextField;
        informationTextField.adjustsFontSizeToFitWidth = YES;
        informationTextField.clearButtonMode = UITextFieldViewModeAlways;

        informationTextField.tag = indexPath.row;

        return informationTextField;
    } 
于 2013-06-06T09:54:59.853 回答
0

像这样将标签设置为 for 循环中的文本字段。

nameTxtFld.tag = 0;
addressTxtFld.tag = 1;
于 2013-06-06T09:36:11.050 回答