【发布时间】:2017-01-25 06:43:42
【问题描述】:
我在单元格中显示 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];
}
【问题讨论】:
-
为什么不对图片使用延迟加载?
-
我正在使用 SDWebImage,它是异步的。
-
但是我需要在图像下载并显示后立即更新高度。
-
你使用了自动布局吗?
-
是的,我正在使用自动布局和自动调整单元格。
标签: ios objective-c uitableview autolayout