Search code examples
iosswiftuitableviewuiimageviewswift-extensions

Refreshing UITableViewCell layout from its imageView after fetching image


I have UIImageView extension that is downloading image from given url in background. I'm using it to fetch images for UITableViewCell. After successful download I'm calling self.setNeedsLayout() but it does not refresh layout of cell since this method is updating layout of callers subviews only. I was trying to use self.superview.setNeedsLayout() or even self.window.setNeedsLayout(). It doesn't work, it says that both of this views are nil. Do You have any ideas how I can refresh UITableViewCell layout from it childView? We are talking about generic UITableViewCell, I would like to avoid creating new class just to make layout works in this case, I would like to use this extension in other places where I just have UIImageView not inside table view. Can It be doable by simple extension? Right now I have custom UITableViewCell that is downloading, setting image and then it is calling self.setNeedsLayout() and it works.


Solution

  • Please consider using librairies such as SDWebImage in order to download images asynchronously. I think it can solve your problem and will improve performances.

    Edit :

    From SDWebImage documentation :

    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!
    • ...