ios - 当第一次设置里面的 imageView 时,自调整单元格没有更新它的高度

标签 ios objective-c uitableview sdwebimage

我在应用程序中遇到了一个烦人的问题,因此为了方便起见,我为此制作了一个演示:

有两个 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/

相关文章:

IOS 切换 AVFoundation 相机

ios - 重命名 Xcode 6 项目 : "linker command failed with exit code 1 (use -v to see invocation)" 后出错

ios - 将 CGRect 表达式分配给 CGFloat - Swift

objective-c - 存档中的 Swift 编译器错误

iphone - UITableView - 如何左对齐?

swift - TableViewController 中的多个自定义单元格

ios - Google AdMob GADBannerView 第一次请求需要很长时间并卡住 UI

ios - 在导航栏中返回时,在 View 上锁定设备方向失败

objective-c - (Swift - OOP) 如何将 Swift (VC1) 中实例化的类转至 Objective C (VC2) 至 Swift (VC3)

iphone - 无法通过自定义方法从 UITableViewCell 获取值