1

I wanna resize imageview'height after download image,but some wrong happened as first cell image when first load tableview;

un normal cell

then if I scroll table to make the cell be in visible again,it go to be normal as second image;

normal cell

I don't known the size of image,can't get from server,how to fix it?

- (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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
JonphyChen
  • 129
  • 1
  • 11

1 Answers1

1

You can set content mode of UIImageView to Aspect Fit.

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

For ref:

How to manage UIImageView content mode?

Community
  • 1
  • 1
Rajesh Dharani
  • 285
  • 4
  • 20
  • `[tableview beginUpdates]; [tableView endUpdates];` will force the table view to refresh the heights in an animated fashion. Below codes work   `if (cacheType == SDImageCacheTypeNone || cacheType == SDImageCacheTypeDisk) { [tableView beginUpdates]; imgViewHeightConstraint.constant = image.size.height * ScreenWidth / image.size.width; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; [tableView endUpdates]; }` – JonphyChen Mar 09 '17 at 08:41