简体   繁体   English

滚动时,根据下载的图像高度,使用UITableViewAutomaticDimension调整自定义表格单元格高度

[英]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.

相关问题 根据下载的图像高度调整表格单元格的高度 - Resize the Table cell height as per downloaded image height 使用UITableViewAutomaticDimension行高自动滚动到表格底部? - Swift,iOS 8+ - Automatically scrolling to the bottom of a table with UITableViewAutomaticDimension row height? - Swift, iOS 8+ 如何使用UITableViewAutomaticDimension更新单元格的高度? - How to update height of the cell with UITableViewAutomaticDimension? 下载图像时,UITableViewCell高度会调整大小 - UITableViewCell height resize when image is downloaded 带有UIImageView的快速自定义UITableViewCell。 需要将单元格调整为图像高度 - Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height UITableViewAutomaticDimension用于表格视图单元格,同时保持图像长宽比 - UITableViewAutomaticDimension for table view cell while keeping image aspect ratio 自定义表格视图单元格滚动时错误的高度 - Custom Table View Cell Drawing Wrong Height When Scrolling 下载映像后调整表格单元格大小 - Resize table cell after image downloaded 确保单元格高度根据自定义UITableViewCell进行调整? - Ensuring cell height adjusts per custom UITableViewCell? UITableview单元格高度根据图像问题 - UITableview cell height as per Image issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM