簡體   English   中英

下載圖像后,重新加載約束或更新自定義表格視圖單元格中的高度

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

我在自己的單元格中顯示了1張圖片,高度需要根據寬高比進行更改。 但是,如果我不重新加載表,約束不會更改,因此像元高度也不會更改。 如果我使用'reloadData'或重新加載特定的行,可以,但是滾動性能會受到影響。 一種方法是先下載所有圖像,但我猜可能不是很好。 我該怎么辦?

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];


}

已知的解決方案是在用戶滾動時不重新加載行。 同時進行主要的布局操作和平滑滾動是不可能的。

因此,當異步圖像下載調用返回時,通過查看表視圖屬性(繼承自UIScrollViewisDraggingisDecelerating來檢測用戶是否正在滾動。 如果用戶正在滾動,則將“要重新加載的索引路徑”放入某些存儲中。 出於以下原因,最好是在表視圖數據源/代理中具有可變的數組屬性。 當用戶停止滾動時,請重新加載路徑並清除存儲。

您可以通過實現UIScrollViewDelegate方法-scrollViewDidEndDecelerating:來完成最后一步。 表視圖的委托也是滾動視圖的委托。 實際上,在滾動停止時看到多個圖像會產生很好的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM