*1. UILabelView高度的自适应
+ (CGFloat)heightWithString:(NSString *)string
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
return rect.size.height;
}
*2. 根据传入的数据模型返回自适应的Cell的高度
+ (CGFloat)cellHeightWithModel:(KBDoStepModel *)model
{
return [KBStepTableViewCell heightWithString:model.describe] + 200;
}
*3. 在对应的控制器里面调用TableView设置Cell的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [KBStepTableViewCell cellHeightWithModel:self.stepMArr[indexPath.row]];
}
*4. 在获取数据的代码里重新加载数据
[GXLRequestManager manager:GET requestWith:stepurl parDic:nil finish:^(NSData *data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDic in dic[@"data"]) {
KBDoStepModel *model = [[KBDoStepModel alloc] init];
[model setValuesForKeysWithDictionary:dataDic];
[self.stepMArr addObject:model];
}
[self.stepTableView reloadData];
} error:^(NSError *error) {
//..
}];
以上代码, 基本可以实现自适应Cell高度的自适应 .