ios – 滚动时使用UITableViewAutomaticDimension按照下载的图像高度调整自定义表格单元格高度

前端之家收集整理的这篇文章主要介绍了ios – 滚动时使用UITableViewAutomaticDimension按照下载的图像高度调整自定义表格单元格高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在运行时获取图像的URL并下载&在表格中显示这些图像.图像以异步方式下载(使用SDWeb Image).更重要的是,我希望以实际尺寸显示所有这些图像.第一次,表加载正常,我有超过5个部分(每个部分包含一行),当我滚动表并尝试使用reloadSections更新部分时,表格显示错误的单元格图像高度.有人可以帮我解决这个问题吗?
注意:我使用的是UITableViewAutomaticDimension

[_imageViewSharedImage sd_setImageWithURL:[NSURL URLWithString:[_popUpManager.popUpDetails valueForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"placeholderBackground"] options:SDWebImageProgressiveDownload completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL)
{
     UIImage *croppedImage=[Util adjustImageSizeWhenCropping:image];     
     [_imageViewSharedImage setFrame:CGRectMake(_imageViewSharedImage.frame.origin.x,_imageViewSharedImage.frame.origin.y,croppedImage.size.width,croppedImage.size.height)];
     [_imageViewSharedImage setImage:croppedImage];
     [self setNeedsLayout];
     [self updateConstraintsIfNeeded];

}];

解决方法

使用UITableViewAutomaticDimension时,必须为单元格内容视图中的所有项目从上到下指定自动布局约束.希望你已经做到了.

在使用自动布局时,您应该注意不要调整框架.如果您想这样做,请使用约束来执行此操作.为imageViewSharedImage设置高度约束和宽度约束的出口,并将其常量设置为所需的值.

_imageViewHeightConstraint.constant = croppedImage.frame.size.height;
_imageViewWidthConstraint.constant = croppedImage.frame.size.width;

然后调用layoutIfNeeded.

猜你在找的iOS相关文章