Reputation: 1143
in iOS 8, I have a UILabel
in a UITableViewCell
. I want the cell to have dynamic height according to UILabel
's text. It works fine. But when the text is a empty string, it seems has a default height, but I want it to be zero.
Here is what I've done:
masonry
. set top
, bottom
constraint.tableView.estimatedHeight
UITableViewAutomaticDimension
for heightForRow
delegate methodSo did I do anything wrong? Any help?
Upvotes: 1
Views: 602
Reputation: 3395
Change your code for heightForRowAtIndexPath to this. This works!
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [yourDataSourceArray objectAtIndex:indexPath.row];
if(text.length > 0 ){
return UITableViewAutomaticDimension;
}
return 0;
}
Upvotes: 2
Reputation: 2850
[yourLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[yourLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
Set your label V compress to a high priority
Upvotes: 0