- 注意:本篇文章只適應(yīng)于單個(gè)Label的cell自適應(yīng)
- 自適應(yīng)cell高度缘薛,主要是UILabel的高度會(huì)有變化酝枢,所以這里主要是說(shuō)說(shuō)label變化時(shí)如何處理涣达,設(shè)置UILabel的時(shí)候注意要設(shè)置preferredMaxLayoutWidth這個(gè)寬度吐葵,還有ContentHuggingPriority為UILayoutPriorityRequried
tableviewCell具體代碼:
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;
self.tempLabel = [UILabel getLabelWithFont:FMedium(20) textColor:C_Black_333333 superView:self.contentView masonrySet:^(UILabel *view, MASConstraintMaker *make) {
make.top.equalTo(self.contentView).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.right.equalTo(self.contentView).with.offset(-10);
make.bottom.equalTo(self.contentView).with.offset(-10);
view.numberOfLines = 0;
view.preferredMaxLayoutWidth = maxWidth;
}];
[self.tempLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
tableviewDelegate返回高度的方法:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
支持版本為iOS 8以上的話才可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回哦污筷。如果需要iOS 8以下版本的自適應(yīng)高度工闺,私信給我。