UITableViewCell - 滚动时ContentView重叠

时间:2014-02-22 22:18:36

标签: ios objective-c uitableview overlapping

我添加了一个UILabel作为我的tableview的内容视图。 UILabel中的文本在滚动时重叠。以下是代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.chatTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Group"];
    static NSString *CellIdentifier = @"Group";

    UITableViewCell *cell = [self.chatTableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                                     forIndexPath:indexPath];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = self.latestTrimText;

    UILabel *cellLabel = [[UILabel alloc]init];
    cellLabel.text = self.dateOfLatestTrim;
    cellLabel.frame = CGRectMake(15, 0, 150, 30);
    [cell.contentView addSubview:cellLabel];

    return cell;
}

我可以通过更改为UITableViewCell *cell = [self.chatTableView dequeueReusableCellWithIdentifier:nil];

来解决此问题

然而,桌面视图的滚动不会很顺利。还有其他方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

tableview单元被回收并可能无限次重复使用。这就是重用标识符所代表的含义。

注意不要在if(cell == nil)块之外添加任何子视图。现在,您只是一次又一次地向同一个单元实例添加新标签。这就是你的文本相互重叠的原因。

我建议您阅读有关如何回收和重复使用细胞的文档。

您的修复程序不正确,因为通过传递nil重用ID,您只是阻止了回收过程。您为tableview的每一行创建一个新实例。这就是你遇到性能问题的原因。