自动布局UILabel的动态高度计算在文本为空字符串时不起作用?



在iOS 8中,我在UITableViewCell中有一个UILabel。我希望单元格根据UILabel的文本具有动态高度。它运行良好但是当文本是一个空字符串时,它似乎有一个默认的高度,但我希望它为零

以下是我所做的:

  1. 使用CCD_ 4设置单元的约束。设置topbottom约束
  2. 设置tableView.estimatedHeight
  3. 返回heightForRow委托方法的UITableViewAutomaticDimension

那么我做错什么了吗?有什么帮助吗?

将heightForRowAtIndexPath的代码更改为。这很管用!

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSString *text = [yourDataSourceArray objectAtIndex:indexPath.row];
   if(text.length > 0 ){
     return UITableViewAutomaticDimension;
   }
   return 0;
}
[yourLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[yourLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];

将标签V压缩设置为高优先级

最新更新