获取图像后从其 imageView 刷新 UITableViewCell 布局

Refreshing UITableViewCell layout from its imageView after fetching image

我有 UIImageView 扩展程序正在后台从给定的 url 下载图像。我正在使用它来获取 UITableViewCell 的图像。下载成功后,我调用 self.setNeedsLayout() 但它不会刷新单元格布局,因为此方法仅更新调用者子视图的布局。我试图使用 self.superview.setNeedsLayout() 甚至 self.window.setNeedsLayout()。它不起作用,它说这两个视图都是零。您有什么想法可以从 childView 刷新 UITableViewCell 布局吗?我们正在谈论通用 UITableViewCell,我想避免创建新的 class 只是为了在这种情况下使布局有效,我想在其他只有 [=10= 的地方使用此扩展] 不在 table 视图内。可以通过简单的扩展来实现吗?现在我有自定义 UITableViewCell 正在下载、设置图像,然后它正在调用 self.setNeedsLayout() 并且它有效。

请考虑使用 SDWebImage 等库以异步下载图像。我认为它可以解决您的问题并提高性能。

编辑:

来自 SDWebImage 文档:

It provides:

  • An UIImageView category adding web image and cache
  • An asynchronous image downloader
  • An asynchronous memory + disk image caching with automatic cache expiration handling
  • A guarantee that the same URL won't be downloaded several times
  • A guarantee that bogus URLs won't be retried again and again
  • A guarantee that main thread will never be blocked
  • Performances!
  • ...