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;
}