cell 自适应高度

cell自适应高度

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````比较多  就会出现偶尔卡顿的情况

2.把计算cell 的高度写 到模型里 这样 cell的高度酒只算了一次
- (CGFloat)cellHeight
{
if (!_cellHeight) {
// 文字的最大尺寸
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * XMGTopicCellMargin, MAXFLOAT);
// 计算文字的高度
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;

}

还有一种更简单的方法
  1. label 的约束必须要有距离底部的高度
  2. self.tabView.estimatedRowHeight = 40;//必须要有估计高度
  3. self.tabView.rowHeight = UITableViewAutomaticDimension;
这是iOS8.0 以后的方法  适用于 所有的cell 都一样  那种
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容