Fry
Fry

Reputation: 6275

iOS UITableViewCell autosizing bug

I've a UITableViewCell designed with .xib that use cell autosizing. In iOS 9 works well, but in iOS 8 cell doesn't expands itself, and the label inside remains with 1 line of text. If the cell go away from the screen and the came back (i.e. after a scrolling) all label are ok.

Ideas? I think this is an iOS 8 bug.

enter image description here enter image description here

Upvotes: 4

Views: 125

Answers (3)

Ajumal
Ajumal

Reputation: 1048

I had faced same problem. If your constraints are set properly you need to set preferredMaxLayoutWidth for each UILabel you have in cell, just before you return the cell. Here is some example code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.lblTitle.text = @"N";
    cell.lblDetails.text = @"bla bla";
    cell.lblOther.text = @"other text";


    // Configure the cell...
    CGfloat leftPading =// "your logo width + spacing between logo and label"
    CGfloat rightPading = //your label trailing space
    cell.lblTitle.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)-(leftPading +rightPading);
    cell.lblDetails.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)-(leftPading +rightPading);
     [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
    }

Have a look at this to know how to put constrains properly

hope this will helps you.

Upvotes: 3

Ilan Kutsman
Ilan Kutsman

Reputation: 469

That depends on your code but there's a really simple way to do this

1. Define auto layout constraints for your prototype cell
2. Specify the estimatedRowHeight of your table view
3. Set the rowHeight of your table view to UITableViewAutomaticDimension

a good explanation can be found here http://www.appcoda.com/self-sizing-cells/

edit:well, I guess the joke is on me for assuming the question was about swift instead of obj-c(didn't notice the tag). Because I think my answer is still useful(for other people that is)...I'm going to leave it here.

Upvotes: -1

Yalamandarao
Yalamandarao

Reputation: 3862

you need to added table view delegated methods for height of the cell.

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

It will work. If there any issue that might be issue on auto layout constraints.

Upvotes: -1

Related Questions