0

我在每个 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;

}

4

2 回答 2

1

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

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

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

于 2012-05-24T09:44:46.913 回答
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;
于 2012-05-24T09:45:40.717 回答