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.
Asked
Active
Viewed 105 times
0

AnthoPak
- 4,191
- 3
- 23
- 41

Prettygeek
- 2,461
- 3
- 22
- 44
1 Answers
0
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!
- ...

AnthoPak
- 4,191
- 3
- 23
- 41
-
I'll try to do this. I just wanted to self code everything:) – Prettygeek Jun 20 '16 at 16:49
-
I understand, I used to do this but I ended up to use this library because it give you many cool features such as image caching. With a single line you'll have your image downloaded asynchronously and you'll not have to think about it anymore. I've replaced all my images download with it and my app gain a lot in efficiency :) – AnthoPak Jun 20 '16 at 17:52
-
Hi, I have read about this library. Looks great but still I would like to know how to make my extension working. Just to know how enforce relayout of cell from it childview. – Prettygeek Jun 23 '16 at 08:30
-
I don't really understand, you want something like `childview.superview.setNeedsLayout` ? – AnthoPak Jun 23 '16 at 08:32
-
Yep. And it is not working. In case of cell. It is not relayouting – Prettygeek Jun 23 '16 at 08:35
-
Not very beautiful but you can try using KVO to call a refresh function in your `UITableView` or `UITableViewCell`. – AnthoPak Jun 23 '16 at 08:37
-
Not nice. And if I want it to be generic extension? And work everywhere? Not just in one case of tableviev? Just extension for uiimageview that is able to fetch image in background, set it and make it visible/ relayout. I can try to get window from app delegate and relayout whole screen. But it sounds like bad idea. – Prettygeek Jun 23 '16 at 08:41
-
Indeed it's sounds like a bad idea. I don't really understand what you're trying to implement, I think a simpler solution should exists... – AnthoPak Jun 23 '16 at 08:44
-
I'll post my extension later. Maybe We will find solution. – Prettygeek Jun 23 '16 at 09:21