1

我有一个UITableView用于输入用户名/密码的。但是当我单击按钮时,我无法获取值。这就是我制作表格的方式:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *kCellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:kCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;

        if ([indexPath section] == 0) {
            UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
            playerTextField.adjustsFontSizeToFitWidth = YES;
            playerTextField.textColor = [UIColor blackColor];
            if ([indexPath row] == 0) {
                playerTextField.placeholder = @"example@gmail.com";
                playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
                playerTextField.returnKeyType = UIReturnKeyNext;
            }
            else {
                playerTextField.placeholder = @"Required";
                playerTextField.keyboardType = UIKeyboardTypeDefault;
                playerTextField.returnKeyType = UIReturnKeyDone;
                playerTextField.secureTextEntry = YES;
            }
            playerTextField.backgroundColor = [UIColor whiteColor];
            playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
            playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            playerTextField.textAlignment = NSTextAlignmentLeft;
            playerTextField.tag = 0;

            playerTextField.clearButtonMode = UITextFieldViewModeNever;
            [playerTextField setEnabled: YES];

            playerTextField.backgroundColor = [UIColor clearColor];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            [cell addSubview:playerTextField];

        }
    }
    if ([indexPath section] == 0) { // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Email";
        }
        else {
            cell.textLabel.text = @"Password";
        }
    }
    return cell;
}

这就是我在按钮点击中尝试过的:

...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
        UITextField* aField = (UITextField *)[aCell viewWithTag:0];
        NSLog(aField.text);
    }
..

这只会打印我:电子邮件密码(标签不是我输入的数据)。如何在此处获取输入的值?

4

2 回答 2

4

所有视图都默认tag为 0。因此,[aCell viewWithTag: 0]返回第一个带有标记 0 的视图——在这种情况下是单元格的textLabel——它UILabel也响应.text. 如果您将您的标签设置cellForRowAtIndexPath:为零以外的值,它应该开始为您工作。

于 2013-09-04T09:37:18.487 回答
1

在这种情况下,您可以将标签提供给像
playerTextField.tag = 115
这样的文本字段,并且您可以按标签查看
UITextField *aField = (UITextField *)[aCell viewWithTag:115];
并显示NSLog(@"text:%@",aField.text);

于 2013-09-04T13:46:30.870 回答