TableView单元格中的UITextField显示问题

时间:2012-05-24 09:39:28

标签: ios xcode uitextfield tableview cell

我为每个UITextField创建了至少10个单元格,用于注册页面。当我在第一个单元格中的文本字段上插入单词时,当我滚动tableView时,另一个单元格中的文本字段显示我刚输入的单词。

以下是我的代码。我输入的textField数据是循环的,这是由dequeueReusableCellWithIdentifier引起的......我该如何解决这个问题?非常感谢你。

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


static NSString *CellIdentifier = @"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
if (cell == nil)
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];


if(indexPath.section == 0){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;

    UITextField *valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0];
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    valTxtField.delegate = self;
    valTxtField.returnKeyType = UIReturnKeyDone;
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    if(indexPath.row == 0)
    {
        valTxtField.text = @"";
        emailTxtFld = valTxtField; //emailTxtFld is global variable
    }
    if(indexPath.row == 1)
    {
        valTxtField.text = @"";
        reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
    }

    [cell.contentView addSubview:valTxtField];
    [valTxtField release];
}    
else if(indexPath.section == 1){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;
    cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
    if(indexPath.row == 0){
        NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
        NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
        cell.registerLabel.text = mainLabel;
        cell.registerTextField.enabled = NO;
    }
}


return cell;

}

2 个答案:

答案 0 :(得分:1)

一种简单的方法是在添加子视图之前从单元格contentView中删除所有子视图,例如:

for (UIView *subview in [cell.contentView subviews])
    [subview removeFromSuperview];

更有效的方法是在if(cell == nil)语句中创建所有单元格,但这取决于表格中有多少单元格。

答案 1 :(得分:0)

正确的实现是在if(cell == nil)中移动任何视图的创建 像这样:

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


static NSString *CellIdentifier = @"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

UITextField *valTxtField;
if (cell == nil)
{
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];

if(indexPath.section == 0){
    valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0];
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    valTxtField.delegate = self;
    valTxtField.returnKeyType = UIReturnKeyDone;
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    valTxtField.tag = 100;

    [cell.contentView addSubview:valTxtField];
    [valTxtField release];
}
}

valTxtField = (UITextField *)[cell.contentView viewWithTag:100];


if(indexPath.section == 0){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;

    if(indexPath.row == 0)
    {
        valTxtField.text = @"";
        emailTxtFld = valTxtField; //emailTxtFld is global variable
    }
    if(indexPath.row == 1)
    {
        valTxtField.text = @"";
        reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
    }
}    
else if(indexPath.section == 1){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;
    cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
    if(indexPath.row == 0){
        NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
        NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
        cell.registerLabel.text = mainLabel;
        cell.registerTextField.enabled = NO;
    }
}


return cell;