Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating UITableViewCell subview's frame has no effect

I have a custom (subclassed) UITableViewCell which contains a few UILabels, a UIButton, and a NSBlock as properties. This subclass is called ExploreCell. Two of the properties are UILabels and are named waitLabel and lastUpdateLabel respectively.

When the contents of lastUpdateLabel is nil or is is blank i.e. @"", I need to move the waitLabel vertically down (on the Y axis) 10 pixels. I am doing this by checking some objects in an NSDictionary as shown in the following code which I have put in the -tableView:cellForRowAtIndexPath: method.

CGRect frame = cell.waitLabel.frame;

if ([[venue allKeys] containsObject:@"wait_times"] && ([[venue objectForKey:@"wait_times"] count] > 0)) {

    frame.origin.y = 43;

}

else {
    frame.origin.y = 53;
}

[cell.waitLabel setFrame:frame];

However, this code intermittently works, and having tried to call -setNeedsLayout, still does not work. By intermittently, I mean that after scrolling a few times, one or two of the cells that match the criteria to move the cell's origin down 10 pixels, actually have their waitLabel's frame changed.

Please can you tell me why this is occurring, and how it can be fixed.

like image 782
max_ Avatar asked Mar 20 '13 21:03

max_


1 Answers

This looks like a problem with auto layout. If you didn't explicitly turn it off, then it's on. You should make an outlet to a constraint to the top (or bottom) of the cell and change the constant of that constraint in code rather than setting frames. According to the WWDC 2012 videos, you shouldn't have any setFrame: messages in your code if you're using auto layout.

like image 148
rdelmar Avatar answered Nov 16 '22 12:11

rdelmar