美文网首页
OC TableView 根据内容获取高度 3

OC TableView 根据内容获取高度 3

作者: 简书花花 | 来源:发表于2018-09-29 17:26 被阅读11次

根据内容计算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;
    }
}

相关文章

网友评论

      本文标题:OC TableView 根据内容获取高度 3

      本文链接:https://www.haomeiwen.com/subject/nxsboftx.html