简体   繁体   中英

autolayout UILabel's dynamic height calculation doesn't work when text is empty string?

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:

  1. set cell's constraints using masonry . set top , bottom constraint.
  2. set tableView.estimatedHeight
  3. return UITableViewAutomaticDimension for heightForRow delegate method

So did I do anything wrong? Any help?

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;
}
[yourLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[yourLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];

Set your label V compress to a high priority

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM