Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change frame of a UILabel in a UITableViewCell when first appearing (using Autolayout)

I have a prototype cell inside my UITableView which contains a UILabel. I want to dynamically change the size of the label (and the cell) depending on the size of the text in the label.

I create the prototype cell in cellForRowAtIndexPath like this:

static NSString *CellIdentifier = @"ProgramDetailCell";
ProgramDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.descriptionLabel.text = self.program.subtitle;
return cell;

Then my ProgramDetailCell has the following implementation:

@implementation ProgramDetailCell
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.descriptionLabel sizeToFit];
}
@end

The first time the cell is displayed, layoutSubviews is called, but the descriptionLabel does not get resized. However, if I scroll down the table and back up again, the "reused" cell appears with the label correctly resized!

Why does it not work the first time the cell is displayed - and what do I need to do to fix it.

Thanks in advance!

like image 567
theDuncs Avatar asked Jan 08 '14 12:01

theDuncs


1 Answers

In xib, go to first tab, under Interface Builder Document , uncheck use Auto Layout box.

like image 53
Yllow Avatar answered Nov 05 '22 00:11

Yllow