0

I've gone through this question on stack overflow and still can't get it to work. The cells don't seam to resize and there for the labels don't become multiline because they are taking into account the bottom constraint.

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Here is my code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    NSDictionary *dict = [self.visitorsGuideArray objectAtIndex:indexPath.row];
    NSLog(@"%@",dict);
    cell.headerLabel.text = [dict valueForKey:@"header"];
    cell.contentLabel.text = [dict valueForKey:@"content"];
    cell.footerLabel.text = [dict valueForKey:@"footer"];

    NSLog(@"%f",cell.frame.size.height);
    cell.headerLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds);
    cell.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds);
    cell.footerLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds);
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];


    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    height += 1.0f;

    return height;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}
Community
  • 1
  • 1
user1898829
  • 3,437
  • 6
  • 34
  • 62
  • Can you please provide a snapshot – Himanshu Joshi Mar 05 '14 at 13:29
  • Does the content in label seems correct? – Himanshu Joshi Mar 05 '14 at 13:32
  • No the content in the label is not correct. It gets truncated after two lines. I assume the label doesn't resize because the cell size doesn't resize and I have a bottom constraint to the cell. It might be a case of the labels need the size of the label to change and the cell needs the size of label to change so its a chicken and egg problem that I don't know how to solve – user1898829 Mar 05 '14 at 14:10

0 Answers0