我在应用程序中遇到了一个烦人的问题,因此为了方便起见,我为此制作了一个演示:
有两个 UIViewController A 和 B。B 从 A 推送,并包含一个使用自调整大小单元格的 UITableView。在UITableViewCell中,有一个UIImageView,它设置了单元格的顶部、前导、底部和尾随约束。因此,当设置该 ImageView 的图像时,单元格高度将自动确定。
但是,当第一次推送B并更新其内容时,单元格高度不正确。但如果弹出 B 然后再次按下 B,单元高度将是正确的。
因此,在我的应用中,如果不进行第二次推送,像 B 这样的每个场景都无法获得正确的单元格大小。
相关代码如下。整个演示可以在Github中找到
#import "AnotherViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Ex.h"
#import "CustomCell.h"
static NSString * const kImageUrl = @"http://img02.tooopen.com/images/20151228/tooopen_sy_152967398112.jpg";
@implementation AnotherViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 300;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
__weak typeof(CustomCell) *wCell = cell;
[cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:kImageUrl] placeholderImage:[UIImage imageNamed:@"default_cover"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
UIImage *scaledImage = [UIImage gs_scaledImageWithWidth:[UIApplication sharedApplication].keyWindow.bounds.size.width image:image];
wCell.bigImageView.image = scaledImage;
[wCell setNeedsUpdateConstraints];
}];
return cell;
}
@end
最佳答案
您应该更新表格,而不是单元格。 表格 View 有一些功能,只是更新行高。 只需依次调用这些方法即可:
[tableView beginUpdates];
[tableView endUpdates];
关于ios - 当第一次设置里面的 imageView 时,自调整单元格没有更新它的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36266152/