简体   繁体   中英

iOS UITableView cells getting duplicated

I have a tableviewcontroller that has dynamic controls created in cells. If it's a dropdown type, I take the user to a different tableviewcontroller to select the value. Once selected, I pop back and reload the data, but when I do that it overwrites the cells on top of one another. I know this is because I'm reusing the cells, but I cannot seem to figure out how to prevent it.

- (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;
}

在此处输入图片说明

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

I'd suggest you to create custom UITableViewCell subclass and put all subviews related logic there.
Next, in order to reset/clear cell before reuse - you should override prepeareForReuse function.

Swift:

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial state here
}

First,I suggest you to use custom cells.If not and your cells are not so many,maybe you can try unique cell identifier to avoid cell reuse:

- (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;
}

Hope it's helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM