在iOS 8中,我在UITableViewCell
中有一个UILabel
。我希望单元格根据UILabel
的文本具有动态高度。它运行良好但是当文本是一个空字符串时,它似乎有一个默认的高度,但我希望它为零
以下是我所做的:
- 使用CCD_ 4设置单元的约束。设置
top
、bottom
约束 - 设置
tableView.estimatedHeight
- 返回
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压缩设置为高优先级