简体   繁体   English

下载图像后,重新加载约束或更新自定义表格视图单元格中的高度

[英]Reload constraint or update height in self-sizing tableview cell after image is downloaded

I show 1 image in my cell and height need to be changed according to aspect ratio. 我在自己的单元格中显示了1张图片,高度需要根据宽高比进行更改。 But if I don't reload table, constraint is not changed and thus cell height too. 但是,如果我不重新加载表,约束不会更改,因此像元高度也不会更改。 If I use 'reloadData' or reload particular row, it is okay but scrolling performance is affected. 如果我使用'reloadData'或重新加载特定的行,可以,但是滚动性能会受到影响。 One of the ways is to download all images first but might not be good I guess. 一种方法是先下载所有图像,但我猜可能不是很好。 How shall I do? 我该怎么办?

Media *media = self.post.medias[0];
NSString *imgUrlStr = media.thumbnailUrl;
[self.ivMain allowTapWithMedia:self.post.medias[0]];

UIImage* displayImage = [myCache imageFromDiskCacheForKey:imgUrlStr];

if (displayImage) {
    @try {

        [self.ivMain setImage:displayImage];
        CGFloat ivHeight = (displayImage.size.height/displayImage.size.width) * CGRectGetWidth(self.ivMain.frame);

        if (roundf(ivHeight) != roundf(self.verticalConstraintIvMain.constant))
            [self.verticalConstraintIvMain setConstant:ivHeight];

    } @catch (NSException *exception) {

    }
}
else {
    [self.ivMain setImageWithURL:[NSURL URLWithString:imgUrlStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        //image width > iv width
        //image height .. ?

        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat ivHeight = (image.size.height/image.size.width) * CGRectGetWidth(self.ivMain.frame);
            if (roundf(self.verticalConstraintIvMain.constant) != roundf(ivHeight)) {

                // [self.verticalConstraintIvMain setConstant:ivHeight];

                id view = [self superview];

                while (view && [view isKindOfClass:[UITableView class]] == NO) {
                    view = [view superview];
                }

                UITableView *tableView = (UITableView *)view;
                [tableView reloadData];
                // [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
            }
        });
    } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];


}

The known solution is to not reload rows while the user is scrolling. 已知的解决方案是在用户滚动时不重新加载行。 It's impossible to have major layout operations and smooth scrolling simultaneously. 同时进行主要的布局操作和平滑滚动是不可能的。

So when the async image-download call comes back, detect if the user is scrolling by looking at the table view properties (inherited from UIScrollView ) isDragging and isDecelerating . 因此,当异步图像下载调用返回时,通过查看表视图属性(继承自UIScrollViewisDraggingisDecelerating来检测用户是否正在滚动。 If the user is scrolling, instead put the 'index path to reload' into some storage. 如果用户正在滚动,则将“要重新加载的索引路径”放入某些存储中。 A mutable array property on whatever is the table view data source/delegate would be best, for reasons below. 出于以下原因,最好是在表视图数据源/代理中具有可变的数组属性。 When the user stops scrolling, reload the paths and clear the storage. 当用户停止滚动时,请重新加载路径并清除存储。

You can do the last step by implementing the UIScrollViewDelegate method -scrollViewDidEndDecelerating: . 您可以通过实现UIScrollViewDelegate方法-scrollViewDidEndDecelerating:来完成最后一步。 The table view's delegate is also the scroll view's delegate. 表视图的委托也是滚动视图的委托。 It can have a nice effect in practice to see multiple images appear when scrolling stops. 实际上,在滚动停止时看到多个图像会产生很好的效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新自调整表格视图的高度? - How to update self-sizing tableview's height? 图像下载和高度约束迅速改变后如何更新tableview单元格高度? - How to update tableview cell height after image downloaded and height constraint changed swift? 根据两个子视图的高度自动调整TableView单元的大小 - Self-Sizing TableView Cells based on two subview's height 自调整tableview单元格中UILabel中的行数 - Number of lines in UILabel in self-sizing tableview cell 自调整表格单元格中的ImageView在应用Aspect Fit之前使用图像的高度 - ImageView in self-sizing table cell uses height of image before Aspect Fit is applied 使用文本视图更新自调整大小的表格视图单元格的高度 - Updating the Height of a Self-Sizing Table View Cell with a Text View 基于使用SDWebImage(AUTOLAYOUT)下载的图像高度更改imageView高度约束后,TableView单元格未调整大小 - TableView cell not resizing after changing imageView height constraint based on Image height downloaded using SDWebImage (AUTOLAYOUT) Xcode 9中的自定义表格查看单元格 - Self-Sizing Table View Cell in Xcode 9 具有图像和标签的自定义TableView单元格的大小 - Self sizing TableView cell with image and label 为什么自定义表格视图单元格中的UIImageView具有内容模式AspectFit的原始高度? - Why UIImageView in self-sizing table view cell has original height with content mode AspectFit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM