我想在下载图像后调整imageview'高度,但是在第一次加载tableview时,第一个单元格图像出现了一些错误;
然后如果我滚动表格使单元格再次可见,它将作为第二个图像正常;
我不知道图像的大小,无法从服务器获取,如何修复它?
- (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;
}
答案 0 :(得分:1)
您可以将UIImageView
的内容模式设置为Aspect Fit。
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
对于参考: