Iphone 分组的TableView文本字段存在问题

Iphone 分组的TableView文本字段存在问题,iphone,tableview,Iphone,Tableview,我在使用分组UITableView使文本字段返回某些内容(而不是null)时遇到问题 这是我的密码: #pragma mark Table view methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableVie

我在使用分组UITableView使文本字段返回某些内容(而不是null)时遇到问题

这是我的密码:

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}

// Customize the appearance of table view cells.
- (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];
}

//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.

if ([indexPath section] == 0) {

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

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

    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {

            nbcUser.placeholder = @"user@nbcuni.com";
            nbcUser.keyboardType = UIKeyboardTypeEmailAddress;
            nbcUser.returnKeyType = UIReturnKeyNext;
        }
        if ([indexPath row] == 1) {

            nbcPass.placeholder = @"required";
            nbcPass.keyboardType = UIKeyboardTypeDefault;
            nbcPass.returnKeyType = UIReturnKeyDone;
            nbcPass.secureTextEntry = YES;
        }
    }

    nbcUser.backgroundColor = [UIColor whiteColor];
    nbcUser.autocorrectionType = UITextAutocorrectionTypeNo;
    nbcUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
    nbcUser.textAlignment = NSTextAlignmentLeft;

    nbcUser.delegate = self;

    nbcPass.backgroundColor = [UIColor whiteColor];
    nbcPass.autocorrectionType = UITextAutocorrectionTypeNo;
    nbcPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
    nbcPass.textAlignment = NSTextAlignmentLeft;
    nbcPass.delegate = self;

    nbcUser.clearButtonMode = UITextFieldViewModeNever;
    nbcPass.clearButtonMode = UITextFieldViewModeNever;

    [nbcUser setEnabled:YES];
    [nbcPass setEnabled:YES];

}
if ([indexPath section] == 0) { 
    if ([indexPath row] == 0) {

        cell.textLabel.text = @"Email";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell addSubview:nbcUser];
    }

    else {

        cell.textLabel.text = @"Password";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell addSubview:nbcPass];
    }
}

return cell;

}

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

if (textField == self.nbcPass) {

    NSString *loginsaved = nbcUser.text;
    NSLog(@"%@", loginsaved );

    [textField resignFirstResponder];
    return NO;
}

return YES;
}

还有人遇到过这个问题吗?当我在该字段中键入内容并返回键盘时,loginsaved将返回null。

-cellforrowatinexpath
:可能会对同一
单元格索引路径调用多次(由于单元格重复使用,文档中对此进行了详细解释)

这意味着您可能在同一单元格中添加多个
UITextFields
,这可能会导致错误


在控制器中创建单元格的布局一开始就是错误的,因此我建议您创建一个UITableViewCell子类,并在那里完成这项工作。这将产生一个组织得更好的代码,帮助您找出问题所在。

如果您已经解决了问题,请不要编辑它。写一个答案,然后接受这个答案。