*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高度的自適應 .