cell.contentView systemLayoutSizeFittingSize:不适用于dynamic高度tableview

我试图在自定义的uitableviewcell中使用自动布局并试图根据这个SO主题来实现dynamic高度

在UITableView中使用自动布局来实现dynamic单元格布局和可变行高

但我的单元格是由xib文件创build的。 所以方法有些不同这里是我的代码在heightForRowAtIndexPath

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { myDataItem *item= [[data items] objectAtIndex:[indexPath row]]; CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"]; if (!cell) { cell = [CustomCell alloc] init]; [self.offscreenCells setObject:cell forKey:@"CustomCell"]; } [cell.commentView setText:item.comment]; [cell.displayNameView setText:item.displayNameOfItemOwner]; [cell.timeAgoView setText:item.timeAgo]; [cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]]; cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds)); [cell setNeedsLayout]; [cell layoutIfNeeded]; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return height+1; } 

我得到的高度[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]总是“无”当我试图debugging,我发现我的customcell的所有子视图是“零”,但单元格本身不是“零”。可能是因为我的customCell是由nib创build的,当我做[[CustomCell alloc] init]时,单元格没有完全创build。

但我曾尝试将方法改为cell = [_tableView dequeueReusableCellWithIdentifier:@“CustomCell”];

结果仍然是一样的。 每个子视图都是零。 这使高度返回为零,并使我的tableview显示不正确。 有什么办法解决这个问题吗?

我试图在AutoLayout上做这些事情。实际上,在之前的版本中,我没有使用AutoLayout并手动计算单元格高度,它完美地工作。 无论如何,我只是想尝试AutoLayout。

请帮忙。提前感谢

这里是我的解决scheme我通过NSArray分配新单元格* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@“CustomCell”owner:self options:nil]; //抓取一个指向第一个对象的指针(大概是自定义单元格,因为这是所有的XIB应该包含的)。

  cell = [topLevelObjects objectAtIndex:0]; -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { myDataItem *item= [[data items] objectAtIndex:[indexPath row]]; CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"]; if (!cell) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). cell = [topLevelObjects objectAtIndex:0]; [self.offscreenCells setObject:cell forKey:@"CustomCell"]; } [cell.commentView setText:item.comment]; [cell.displayNameView setText:item.displayNameOfItemOwner]; [cell.timeAgoView setText:item.timeAgo]; [cell.imageView setImage:[UIImage imageNamed:@"image"]]; cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds)); [cell setNeedsLayout]; [cell layoutIfNeeded]; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return height+1; } 

据我所知,在iOS 8下,所提出的解决scheme不适用于具有多行标签的单元,因为systemLayoutSizeFittingSize将不考虑所需的单元宽度(简单地说,它会尝试将标签布置成单行,因此使细胞比它应该宽)。

我设法通过在单元格的contentView安装一个宽度约束来解决这个问题,而不是设置单元格边界。 记得在调用systemLayoutSizeFittingSize:之后立即卸载这个约束systemLayoutSizeFittingSize: