为什么我的textFieldDidEndEditing标记不起作用?

时间:2013-06-06 09:31:48

标签: ios uitableview tags uitextfield

美好的一天!

我有一个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 占位符以显示其对应的标记,并且其工作正常。

截图:

enter image description here

问题出在我的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

Apple's Documentation

2 个答案:

答案 0 :(得分:1)

您正在调用 createInformationTextField 方法来创建文本字段。我想你是从每个单元格调用这个方法。因此,当您调用此方法时,它将进入for循环,并在两个循环之后,返回带有标记1的textfield,返回每个单元格。这就是为什么你得到每个文本字段的标记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;
    } 

答案 1 :(得分:0)

将您的代码设置为for循环中的文本字段。

nameTxtFld.tag = 0;
addressTxtFld.tag = 1;