iOS UITableView单元格重复

iOS UITableView单元格重复,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我有一个tableviewcontroller,它在单元格中创建了动态控件。如果是下拉类型,我会将用户带到另一个tableviewcontroller来选择该值。一旦选中,我会弹出并重新加载数据,但当我这样做时,它会覆盖彼此上方的单元格。我知道这是因为我在重复使用细胞,但我似乎不知道如何防止它 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; [self.tableView reloadDat

我有一个tableviewcontroller,它在单元格中创建了动态控件。如果是下拉类型,我会将用户带到另一个tableviewcontroller来选择该值。一旦选中,我会弹出并重新加载数据,但当我这样做时,它会覆盖彼此上方的单元格。我知道这是因为我在重复使用细胞,但我似乎不知道如何防止它

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    [self.tableView reloadData];

}


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


    EWHInboundCustomAttribute *ca = [visibleCustomAttributes objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.tag=indexPath.row;


    if (ca.CustomControlType == 1) {
        cell.detailTextLabel.hidden=true;
        cell.textLabel.hidden=true;

        UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];

            caTextField.adjustsFontSizeToFitWidth = YES;
            caTextField.textColor = [UIColor blackColor];

            caTextField.placeholder = ca.LabelCaption;
            if (ca.ReadOnly) {
                [caTextField setEnabled: NO];
            } else {
                [caTextField setEnabled: YES];
            }
            caTextField.text=nil;
            caTextField.text=ca.Value;
            caTextField.tag=indexPath.row;

            caTextField.delegate=self;

            [cell.contentView addSubview:caTextField];



    } else if (ca.CustomControlType == 4) {

        cell.detailTextLabel.text=ca.Value;
        cell.textLabel.text=ca.LabelCaption;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    } else {

            cell.detailTextLabel.hidden=true;
            cell.textLabel.hidden=true;
            UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
            caTextField.adjustsFontSizeToFitWidth = YES;
            caTextField.textColor = [UIColor grayColor];

            caTextField.placeholder = ca.LabelCaption;
            [caTextField setEnabled: NO];
            caTextField.text = ca.Value;

            caTextField.tag=indexPath.row;
            caTextField.delegate=self;
            [cell.contentView addSubview:caTextField];
    }



    return cell;
}

我建议您创建自定义的
UITableViewCell
子类,并将所有与子视图相关的逻辑放在那里。
接下来,为了在重新使用之前重置/清除单元格,您应该覆盖
prepeareforeuse
函数

斯威夫特:

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial state here
}

首先,我建议您使用自定义单元格。如果没有,并且您的单元格不太多,也许您可以尝试使用唯一的单元格标识符,以避免单元格重复使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // unique reuseID
    NSString *cellReuseID = [NSString stringWithFormat:@"%ld_%ld", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseID];
        // do something
    }
    return cell;
}

希望有帮助。

我建议至少使用
[UIView viewWithTag:tag]
捕获相同的UITextfield对象,而不是每次都创建UITextfield。

为了提高性能,可以重用单元格。这就是为什么您的单元格包含以前显示的内容。我建议在谷歌上搜索“uitableviewcell重用”之类的东西,了解它的工作原理。@EmilioPelaez是的,我同意重用单元格。但是,我似乎不知道如何更新uitextfield而不是重新创建它;和[cell.contentView addSubview:caTextField];使用IB为您添加它们。您可以给单元格提供不同的标识符:@“cell”、@“cell1”、@“cell2”等,以便直接从IB加载单元格,但您可以自己添加子视图。@E.Coms您能详细介绍一下吗?我不明白你所说的添加不同标识符是什么意思。你不应该操纵任何单元格的视图层次结构。而是设计不同的单元格,在不同的标识符下注册。