UITableView单元格中的UITextField返回为(null)

问题描述 投票:0回答:1

这两个文本字段位于UItableView中。构建完成没有任何错误。当我输入登录详细信息并点击UINavController的提交按钮时,第一个字段返回为(null)。我无法找到发生这种情况的原因。

这是我正在使用的代码:

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

    // Configure the cell.

    if ([indexPath section] == 0) {
        tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
        tUser.adjustsFontSizeToFitWidth = YES;
        tUser.textColor = [UIColor blackColor];

        tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
        tPass.adjustsFontSizeToFitWidth = YES;
        tPass.textColor = [UIColor blackColor];

            if ([indexPath row] == 0) {
                tUser.placeholder = @"[email protected]";
                tUser.keyboardType = UIKeyboardTypeEmailAddress;
                tUser.returnKeyType = UIReturnKeyNext;
            }
            if ([indexPath row] == 1) {
                tPass.placeholder = @"Required";
                tPass.keyboardType = UIKeyboardTypeDefault;
                tPass.returnKeyType = UIReturnKeyDone;
                [tPass addTarget:self
                                      action:@selector(save:)
                forControlEvents:UIControlEventEditingDidEndOnExit];

                tPass.secureTextEntry = YES;
            }
        }

        tUser.autocorrectionType = UITextAutocorrectionTypeNo;
        tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
        tUser.textAlignment = UITextAlignmentLeft;

        tPass.autocorrectionType = UITextAutocorrectionTypeNo;
        tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
        tPass.textAlignment = UITextAlignmentLeft;

        tUser.clearButtonMode = UITextFieldViewModeNever;
        tPass.clearButtonMode = UITextFieldViewModeNever;

        [tUser setEnabled:YES];
        [tPass setEnabled:YES];

        //[tUser release];
        //[tPass release];

     // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Username";
            [cell addSubview:tUser];
        }
        else {
            cell.textLabel.text = @"Password";
            [cell addSubview:tPass];
        }

    return cell;    
}

-(IBAction) save: (id) sender {

        if ([tPass text] == nil) {
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                            message:@"There was no password entered, please enter the correct password and try again." 
                                                           delegate:self 
                                                  cancelButtonTitle:@"Okay" 
                                                  otherButtonTitles:nil];
            [alertV show];
            [alertV release];
        }
        else {
            NSLog(@"we can do something here soon...");

            //NSString *tUserString = [[NSString alloc] initWithFormat:@"Hello: %@", tUser.text];

            NSLog(@"We saved their username: %@", [tUser text]);
            NSLog(@"We saved their password: %@", [tPass text]);


        }

    }
iphone cocoa-touch uitableview
1个回答
0
投票

一个问题是,只要表需要重新创建单元格,就会不断重新创建文本字段,单个单元格可能会多次发生。您没有显示任何指示以后如何访问这些文本字段以检索值,例如使用控件的标记属性。

当您使单元格出列时,该单元格的子视图将保持不变,因此您应该使用tag属性来检索已存在的文本字段并更新其上的文本,而不是每次都创建新的文本字段。

此外,更好的方法是创建一个自定义UITableViewCell子类,其中包含具有允许您轻松访问嵌入文本字段的属性的文本字段。然后,在tableView:didSelectRowWithIndexPath:方法中,您可以访问所选行的特定单元格。要获取单元格,请将cellForRowAtIndexPath:消息发送到tableView实例。获得单元格后,您可以访问自定义属性以检索用户名和密码。

© www.soinside.com 2019 - 2024. All rights reserved.