根据内容计算cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self heightCellWithIndexPath:indexPath];
}
- (CGFloat ) heightCellWithIndexPath:(NSIndexPath *)indexPath {
if (!_heightCellDic) {
_heightCellDic = [NSMutableDictionary new];
}
if ([[_heightCellDic allKeys] containsObject:indexPath]) {
return [_heightCellDic[indexPath] floatValue];
} else {
NSString *title = _dataAry[indexPath.row];
CGFloat conentWidth = kScreen_Width - leftPadding*2;
CGFloat height = [title boundingRectWithSize:CGSizeMake(conentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NomalFont_M} context:nil].size.height;
NSURL *url= [NSURL URLWithString:_dataAry[indexPath.row]];
if (url) {
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
CGFloat imgH = img.size.height;
if (img.size.width > conentWidth) {
ImgH = conentWidth * imgH /img.size.width;
}
height = height + imgH;
}
[_heightCellDic setObject:@(height) forKey:indexPath];
return height;
}
}
网友评论