[英]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
. 因此,当异步图像下载调用返回时,通过查看表视图属性(继承自
UIScrollView
) isDragging
和isDecelerating
来检测用户是否正在滚动。 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.