Reputation: 31
So this is my problem: I have an UITableView with custom UITableViewCells. Each one of the cells have one UIImageView which downloads an remote image using asynchronous image loading.
It's working ALMOST perfectly, but I have this weird issue.
When the view is loaded I can see the 5 or 6 first cells without scrolling (iPhone's display height).
The problem is: the image of each cell doesn't appear at this moment. Scrolling the table to the bottom, the cells #7~end loads perfectly.
The WEIRD fact is: when I scroll back to the top, now the 5 or 6 cells loads perfectly.
My first thought was: when the view is loaded, the cellForRowAtIndexPath method was not calling loadimage method automatically for the first cells, just after user interaction.
But using NSLog (step by step) to find where is the problem, the thing is:
So, the loadimage method is being called, indeed. But not receiving data.
Scrolling to the bottom and then going back to the top cells, it's all ok. The word is WEIRD.
Upvotes: 1
Views: 2998
Reputation: 1115
Check out HJCache, which is made to make this problem easy, it provides full management of asynchrounous image download, UIImage object sharing, and a file cache that you can configure and can clean itself up: HJCache: iPhone cache library for asynchronous image loading and caching
Upvotes: 1
Reputation: 29160
You can try using my JImage object, which is exactly the same as a UIImageView, but it is passed a URL instead of a file path. While the image loads (full asynch), an activity indicator is displayed. Use it just like you would a UIImageView, but initialize it with a URL.
Implement a kind of white board on Iphone
Upvotes: 1
Reputation: 31
Thanks Tim, Skirwan and Dutchie ! I found the problem. It's so obvious now, omg ! I was just looking at the wrong place !
The data for UITableView comes from a XML. After parsing it, I call reloadData. What I didn't see before is that all methods of this process is being processed inside an NSAutoreleasePool. So, when it finishes (quite fast), all is gone (and the NSURLConnection don't have time enough to load the image).
Upvotes: 0
Reputation: 1372
The most likely explanation is that the asynchronous call is taking too long; if it hasn't returned before a cell is drawn, your cell will draw without it and never redraw until being scrolled off screen and back on.
You can either load your images synchronously before your cells first draw (which probably isn't what you want, as it would hurt UI responsiveness) or you can add some code in your connectionDidFinishLoading: handler that will flag the table as needing to redisplay the cell(s) for the just-loaded image.
Upvotes: 0
Reputation: 60110
You say the NSURLConnection is created, but you never say it starts. Make sure that you're either calling one of the methods that launch the connection (initWithRequest:delegate:startImmediately:
or start
).
If that's not your problem, then can you check if any of the other NSURLConnection delegate methods are being called? Look specifically for connection:didFailWithError:
, as there may be some weird kind of network startup procedure that has to happen before your connections work, causing them to fail on first load but not later.
Finally, can you check to see if it works when you send the table view a reloadData
message? Something like [tableView performSelector:@selector(reloadData) withObject:nil afterDelay:1.0];
, so that it gets reloaded after a one-second wait.
Upvotes: 0