您的位置:首页 > 移动开发 > IOS开发

ios Tableview行高的计算

2016-09-16 16:21 369 查看
一、正常计算行高

这其中需要先传入一个最大尺寸和一个属性字典,特殊的格式要求都写在属性字典中。

NSDictionary *attrs = @{NSFontAttributeName : font};

[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size


二、预估行高

iOS8的新特性

需要适配iOS7的行高计算

_tableView.estimatedRowHeight = 30;
_tableView.rowHeight = UITableViewAutomaticDimension;


三、系统自动计算

iOS 6.0以后都支持

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identify = NSStringFromClass([Case8Cell class]);
Case8Cell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[Case8Cell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
}
Case8DataEntity *entity = _arrayEntity[indexPath.row];
[cell setEntityData:entity indexPath:indexPath click:^() {

}];
//缓存行高
if (entity.cellHeight <= 0) {
entity.cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 0.5f;
}
if (entity.cellHeight < 50) {
return 50;
}
return entity.cellHeight;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitablevie cellHeight