分组TableView文本字段的问题

时间:2013-06-25 20:51:29

标签: 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:(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。

1 个答案:

答案 0 :(得分:0)

-cellForRowAtIndexPath:对于相同的cell index path可能会多次调用(由于单元格重用,文档中对此进行了详细说明)。

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

在控制器中创建单元格的布局是错误的模式,所以我建议你创建一个UITableViewCell子类并在那里工作。这将导致更好的组织代码,帮助您找出问题所在。