[英]While scrolling resize the custom Table cell height with UITableViewAutomaticDimension as per downloaded image height
I am getting the image's URL at run time and download & display these images in a table. 我在运行时获取图像的URL,并在表格中下载并显示这些图像。 Images are downloading asynchronously(using SDWebImage). 图像以异步方式下载(使用SDWebImage)。 What is more important is that I want to display all these images with their actual sizes. 更重要的是,我希望以实际尺寸显示所有这些图像。 First time ,the table is loading fine, I have more than 5 section(each section contains one row) and when I scroll the table and try to update sections by using reloadSections, then the table shows with wrong cell image height. 第一次,表加载正常,我有超过5个部分(每个部分包含一行),当我滚动表并尝试使用reloadSections更新部分时,表格显示错误的单元格图像高度。 Can anybody help me out with this? 有人可以帮我解决这个问题吗? Note: I am using UITableViewAutomaticDimension 注意:我使用的是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];
}];
When using UITableViewAutomaticDimension, you have to specify auto layout constraints from top to bottom for all the items in your cell's content view. 使用UITableViewAutomaticDimension时,必须为单元格内容视图中的所有项目从上到下指定自动布局约束。 Hope you have done that. 希望你已经做到了。
And while using auto layout, you should take care to not adjust the frame. 在使用自动布局时,您应该注意不要调整框架。 If you want to do so, do it using constraints. 如果您想这样做,请使用约束来执行此操作。 Make an outlet for the height constraint and width constraint of your imageViewSharedImage and set its constant to the values you require. 为imageViewSharedImage设置高度约束和宽度约束的出口,并将其常量设置为所需的值。
_imageViewHeightConstraint.constant = croppedImage.frame.size.height;
_imageViewWidthConstraint.constant = croppedImage.frame.size.width;
Then call layoutIfNeeded. 然后调用layoutIfNeeded。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.