Objective-C:自我调整表格查看下载后具有动态图像高度的单元格

时间:2017-03-08 11:18:27

标签: objective-c image uitableview row-height

我想在下载图像后调整imageview'高度,但是在第一次加载tableview时,第一个单元格图像出现了一些错误;

un normal cell

然后如果我滚动表格使单元格再次可见,它将作为第二个图像正常;

normal cell

我不知道图像的大小,无法从服务器获取,如何修复它?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopingTableCell* cell = [tableView dequeueReusableCellWithIdentifier:@"shopingCell" forIndexPath:indexPath];
    StoreDynamicsModel *model = storeArray[indexPath.row];
    cell.contentLabel.text = model.name;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.goods_img_url] placeholderImage:[UIImage imageNamed:@"defaultShort"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        dispatch_async(dispatch_get_main_queue(), ^{

            cell.imgViewHeightConstraint.constant = image.size.height * ScreenWidth / image.size.width;
        });
    }];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 120.f;
    return self.tableView.rowHeight;
}

1 个答案:

答案 0 :(得分:1)

您可以将UIImageView的内容模式设置为Aspect Fit。

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

对于参考:

How to manage UIImageView content mode?