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;
}
还有一种更简单的方法
- label 的约束必须要有距离底部的高度
- self.tabView.estimatedRowHeight = 40;//必须要有估计高度
- self.tabView.rowHeight = UITableViewAutomaticDimension;
这是iOS8.0 以后的方法 适用于 所有的cell 都一样 那种