views:

1168

answers:

5

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:

  1. cellForRowAtIndexPath calls the loadimage method
  2. inside loadimage method, the NSURLConnection is created
  3. NSURLConnection didReceiveData's method is not being called

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.

A: 

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.

Tim
A: 

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.

http://www.iphonedevsdk.com/forum/iphone-sdk-development/29368-implement-kind-white-board-iphone.html#post127357

Dutchie432
A: 

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.

Skirwan
A: 

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).

A: 

Hi, I'm having exactly the same problem. I understand the cause, but I'm a beginner, so could anyone explain me how to solve this problem? Thank you

CoCow