UITableViewCell frame frame update has no effect

I have a custom (subclass) UITableViewCell that contains several UILabels, UIButton and NSBlock as properties. This subclass is called ExploreCell . Two properties are UILabels and are called waitLabel and lastUpdateLabel respectively.

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

 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 works intermittently, and trying to call -setNeedsLayout still does not work. With interruptions, I mean that after scrolling several times, one or two cells that meet the criteria for moving the cell source by 10 pixels actually have a waitLabel frame.

Please tell me why this is happening and how it can be fixed.

+6
source share
3 answers

This looks like a problem with auto-layout. If you have not explicitly disabled it, then it is enabled. You should exit to the restriction to the upper (or lower) cell and change the constant of this restriction in the code, and not set the frames. According to WWDC 2012 videos, you shouldn't have any setFrame: messages in your code if you use auto-layout.

+12
source

You tried to run

 [CATransaction flush]; [CATransaction begin]; 

in the main thread after updating these values?

0
source

You need to use [tableView beginUpdates] + [tableView endUpdates] when you change the layout in any of your cells and want them to be visible right away.

 -(void)someMethod { [tableView beginUpdates]; // your code: 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]; [tableView endUpdates]; } 

Make sure that [tableView beginUpdates] and [tableView endUpdates] are always combined.

-3
source

All Articles