【问题标题】:UITableView adding duplicate labels on top of each otherUITableView 在彼此之上添加重复的标签
【发布时间】:2011-03-30 15:00:22
【问题描述】:

当我向上和向下滚动时,我的 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;
}

【问题讨论】:

  • 与重复标签无关,但您确定要为数组索引执行indexPath.row + indexPath.section 吗?例如,这将为第 0 节 + 第 1 行返回与第 1 节 + 第 0 行相同的索引。

标签: iphone objective-c uitableview duplicates uilabel


【解决方案1】:

您正在使可重复使用的单元格出列,因此 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;
}

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

【讨论】:

  • 这得到 label = [cell.contentView viewWithTag:1];不兼容的指针类型从 UIView 分配 UILabel 我使用 indexPath 部分 + 行,因为每个单元格都在一个单独的部分中,但所有单元格标签文本都在一个数组中。
【解决方案2】:

您只需删除子视图:

for (UIView * view in cell.contentView.subviews)
{
    [view removeFromSuperview];
    view = nil;
}

这将完成“我遇到同样的问题”的工作

【讨论】:

    【解决方案3】:

    我解决这个问题的方法不是很优雅,但很有效。

    正如 Joe 所提到的,问题在于我们通过将单元格出列来重用单元格。这意味着有时我们使用一个已经设置了属性的单元格,例如它的 textLabel。在我的情况下,这是由于细胞结构的差异。它将一个单元格的图像视图重叠在另一个根本不应该有图像的单元格上。

    我发现识别有问题的部分并在 cellForRowAtIndex 开始时将它们设置为 nil 可以解决问题。这相当于在重新使用之前将电池板擦干净。

    这是我的编辑版本:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:bCellIdentifier];
        cell.imageView.image = Nil;
        cell.textLabel.text = Nil;
        _nameField.text = @"";
    
        ...
    
        // Setting the code with regards to the cells here
    
        ...
    
        return cell;
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多