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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With