【问题标题】:UITableViewCell with dynamically added UITextField using addSubview - How to get UITextField values when 'return' hit on keyboard使用 addSubview 动态添加 UITextField 的 UITableViewCell - 如何在键盘上“返回”时获取 UITextField 值
【发布时间】:2011-03-14 12:14:43
【问题描述】:

我使用 UITableView 创建了一个登录屏幕,其中包含一个部分和两个单元格。这就是这些单元格的创建方式。现在我不知道以后如何从这些单元格中检索值。

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

确切地说,我想在用户点击 return 键时检索值。所以,我想在这里获取单元格的文本字段值...

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

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

    return YES;
}

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

【问题讨论】:

    标签: objective-c ios uitableview uitextfield uitextfielddelegate


    【解决方案1】:

    您应该使用@gnuchu 提到的textFieldDidEndEditing: 方法,但是要检测刚刚完成编辑的文本字段,您可以使用以下代码:

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

    上面的代码应该适合你,但是对 2 个固定单元格使用 UITableView 是多余的,只会给你的代码增加不必要的复杂性。 IMO 最好只使用带有 2 个标签和 2 个文本字段的标准视图。这可以很容易地在 Interface Builder 或代码中创建,并且会大大简化事情。

    【讨论】:

    • 感谢克里斯,它适用于一个单元格,但不适用于另一个单元格(无论哪个文本字段当前持有键盘)。检查此代码..记住我想在用户点击“返回”键时获得价值..
    • 明白了,克里斯.. 我将你的代码移到通用函数中,并在 textFieldDidEndEditing 和 textFieldShouldReturn 中调用它(在做其他事情之前)。
    【解决方案2】:

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

    - (void)textFieldDidEndEditing:(UITextField *)textField
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 2011-10-21
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2019-12-28
      相关资源
      最近更新 更多