Jon
Jon

Reputation: 4732

Having issue with reusing UITableViewCell

I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?

  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;

Upvotes: 2

Views: 184

Answers (3)

danh
danh

Reputation: 62676

The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.

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

Upvotes: 1

viggio24
viggio24

Reputation: 12366

Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.

So I would try this:


[aCustomerCell.contentView addSubview:aLine];

If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):


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

Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).

Upvotes: 2

NSIntegerMax
NSIntegerMax

Reputation: 542

try adding it to the contentView

[aCustomerCell.contentView addSubview:aLine]

Upvotes: 0

Related Questions