views:

230

answers:

1

Hi,

I have a subclass of UITableViewCell which contains several elements - UIImageViews, Labels, etc.
Since this cell is intended to be reusable, I want to be able to change it's appearance a bit depending on what data it is currently displaying.

So as an example - I have this view in my custom UITableViewCell:

UIImageView* delimeterView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellDelimiter.png"]];

Which I want to be able to hide sometimes like this:

- (void) setRecord:(id)record__ {  
    if (record__.type == NO_DELIMETER_VIEW)  
        delimeterView.hidden = YES;  
    else  
        delimeterView.hidden = NO;
    [self setNeedsLayout];   
} 

But the problem is that delimeterView will always be displayed on the cell, just like if it was drawn once in the init method and then drawing context was never changed or cleared. I've tried setting clearsContextBeforeDrawing property to YES for both cell and its contentView, I've also tried setting opaque for cell and its contentView to NO since I've read there might be some problems with that aswell in case you're using transparent views.
Nothing helps.
It looks like UITableViewCell never clears its graphic context and just paints over old elements.
Any tips on what am I doing wrong? I know I can probably fix this by doing custom drawing but I'd rather not.

A: 

First, are you sure that delimeterView in setRecord: is actually pointing to your delimeterView? In the code example you give, you assign it to a local. Do you later assign this to an ivar? (You should always use accessors to access ivars: self.delimeterView).

Next, calling -setNeedsLayout just schedules a call to -layoutIfNeeded, which walks the hierarchy calling -layoutSubviews. The default implementation of -layoutSubviews does nothing. You probably meant to call -setNeedsDisplay here, or you need to implement -layoutSubviews to do what you want.

Rob Napier
Ok yes, setNeedsDisplay was actually the right method. Thanks for pointing that out.
Alexey