cell自適應(yīng)高度
1.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath里直接算出cell的高度
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * 10, MAXFLOAT);
// CGFloat textH = [topic.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize].height;//方法棄用
CGFloat textH = [topic.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
每加載一次````cell```` 就算一次,如果````cell````比較多 就會(huì)出現(xiàn)偶爾卡頓的情況
2.把計(jì)算cell 的高度寫 到模型里 這樣 cell的高度酒只算了一次
- (CGFloat)cellHeight
{
if (!_cellHeight) {
// 文字的最大尺寸
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * XMGTopicCellMargin, MAXFLOAT);
// 計(jì)算文字的高度
CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
// cell的高度
_cellHeight = XMGTopicCellTextY + textH + XMGTopicCellBottomBarH + 2 * XMGTopicCellMargin;
}
return _cellHeight;
}
還有一種更簡(jiǎn)單的方法
- label 的約束必須要有距離底部的高度
- self.tabView.estimatedRowHeight = 40;//必須要有估計(jì)高度
- self.tabView.rowHeight = UITableViewAutomaticDimension;
這是iOS8.0 以后的方法 適用于 所有的cell 都一樣 那種