セルに1イメージを表示し、縦横比によって高さを変更する必要があります。しかし、テーブルをリロードしないと、制約は変更されず、セルの高さも変更されません。 'reloadData'を使用するか、特定の行をリロードすると問題ありませんが、スクロールのパフォーマンスには影響します。方法の1つは、すべての画像を最初にダウンロードすることですが、私が推測すると良いとは限りません。どうすればいいですか?イメージをダウンロードした後にセルフサイズテーブルビューセルのリロード制約または更新高さ
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];
}
画像にレイジーローディングを使用してみませんか? –
私はSDWebImageを使用していますが、それは非同期です。 –
しかし、画像がダウンロードされて表示されたら直ちに高さを更新する必要があります。 –