Fry February 2016

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

Answers


Aju February 2016

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.


Ilan Kutsman February 2016

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.


Singapore February 2016

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.

Post Status

Asked in February 2016
Viewed 3,315 times
Voted 6
Answered 3 times

Search




Leave an answer