UITableView将重复的标签添加到彼此之上

use*_*173 3 iphone objective-c duplicates uitableview uilabel

当我向上和向下滚动时,我的UITableView会在彼此的顶部添加重复的标签.因此,最后添加的标签会增加加速减速和崩溃的标签.

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

    UILabel *label;
    label = [[UILabel alloc] initWithFrame:nameFrame];
    [label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
    [label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
    [label setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:label];
    [label release];

    label = [[UILabel alloc] initWithFrame:statusFrame];
    [label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
    [label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextAlignment:(UITextAlignmentRight)];
    [cell addSubview:label];
    [label release];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 8

您正在将可重复使用的单元格出列,因此UILabel已存在于每个出列的单元格中.请尝试以下代码.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UILabel *label;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        label = [[UILabel alloc] initWithFrame:nameFrame];
        label.tag = 1; //Important for finding this label
        [label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
        [label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
        [label setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:label];
        [label release];

        label = [[UILabel alloc] initWithFrame:statusFrame];
        label.tag = 2; //Important for finding this label
        [label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
        [label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextAlignment:(UITextAlignmentRight)];
        [cell.contentView addSubview:label];
        [label release];
    }
    else
    {
        label = (UILabel*)[cell.contentView viewWithTag:1];
        label.text = [name objectAtIndex:indexPath.row + indexPath.section];

        label = (UILabel*)[cell.contentView viewWithTag:2];
        label.text = [status objectAtIndex:indexPath.row + indexPath.section];
    }

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我确实调整了代码以使用单元格的contentView.