我在堆栈溢出时已经完成了这个问题但仍无法使其工作。单元格不会缝合以调整大小,并且标签不会变成多行,因为它们考虑了底部约束。
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
这是我的代码。
- (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;
}