2012-02-21 91 views
2

我有以下代码,它为UITableViewCell绘制分隔线和文本。它看起来很好,但当我滚动屏幕然后返回时,分隔线已消失,但文本仍然正常。有任何想法吗?重复使用UITableViewCell问题

static NSString *aProgressIdentifier = @"CustomerCell"; 
       UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier]; 
       if (!aCustomerCell) { 
        aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease]; 
        aCustomerCell.contentView.backgroundColor = [UIColor whiteColor]; 
        UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)]; 
        aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0]; 
        [aCustomerCell addSubview:aLine]; 
        [aLine release]; 
       } 

       CMACustomer *aCustomerObject = aCellObject; 
       aCustomerCell.textLabel.text = aCustomerObject.customerFullName; 
       aCustomerCell.detailTextLabel.text = nil;  
       aCell = aCustomerCell; 

回答

2

尝试添加“aLine”图像视图作为contentView的子视图,而不是整个表本身。可能当单元格被重用,然后再次调用layoutSubviews时,contentView会重叠(白色背景)您的aLine。事实上,考虑到iOS默认单元格每次在屏幕上显示时都会动态重绘和调整其子视图。

所以我会尝试这样的:


[aCustomerCell.contentView addSubview:aLine]; 

如果这不起作用,你可以做什么是完全删除内容查看,并添加自己的自定义子视图(做到这一点,如果(aCustomerCell内)!除非不在外面,你不会得到的细胞再利用的好处):


if (!aCustomerCell) { 
    aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease]; 
    [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)]; 
    aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0]; 
    [aCustomerCell.contentView addSubview:aLine]; 
    [aLine release]; 
} 

最后另一项检查验证室高度为> 72(这似乎是一个微不足道的检查,但其经常头痛的源泉! )。

+0

雅的高度恰好是72,并且该线最初显示,这意味着它的工作原理。我试过你发布的第二个代码,但它只给我所有没有文本或任何东西的灰色单元格。 – Jon 2012-02-21 21:16:27

+0

我实际上已将行改为71,现在所有作品。奇怪。 – Jon 2012-02-21 21:39:19

0

尝试将其添加到内容查看

[aCustomerCell.contentView addSubview:aLine] 
+0

这没有奏效,我用完整的方法更新了代码,因为问题在别处。 – Jon 2012-02-21 20:12:08

1

表视图使用细胞池,所以你不能确保你得到任何给定的索引路径是哪一个。您可以使用单元格或内容视图,但一定要为每个单元添加一条自定义行。

UIImageView *aLine = (UIImageView *)[cell viewWithTag:64]; 
if (!aLine) { 
    // etc. 
    UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)]; 
    aLine.tag = 64; 
    [cell addSubview:aLine]; 
    // 
} 
// other formatting logic here, you can also hide/show aLine based on biz logic 
+0

我试过但没有运气。 – Jon 2012-02-21 20:09:45

+0

我用完整的方法更新了代码,因为问题在别处。 – Jon 2012-02-21 20:11:58

+0

乔恩 - 我的坏。看到我修改后的答案。 – danh 2012-02-21 20:13:20