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 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.