views:

815

answers:

4

I have a UITableView subclass and a UITableViewCell sublass that I'm using for cells. I'm bulding all my cells in advance and store them in an array from where I use them in cellForRowAtIndexPath. Aside from this I have a thread that loads some images in each cell, in the background. The problem is that the cells don't get refreshed as fast as the images are loaded. For example, if I don't scroll my table view, the first cells only get refreshed when all cells have been modified and the thread has exited.

Any ideas on how I can effectively refresh my tableview/cell?

A: 

Are you using a callback to notify the controller of your tableview that the images have been loaded? If not, that would be an ideal method.

When the image loads, fire off a callback to the table view controller that sets the image on the cell, and then calls reloadData on the tableView. This way whenever a new image loads, the table will update to display it.

Jasarien
Jasarien is right, you should be using a delegate of some sort to callback to your ViewController updating the Image on the cell. You then can call reloadData or setNeedsDisplay on the cell. You should also consider NOT making your rows in advance. This will kill performance and use a ton of memory depending on the number of cells. You should be re-using a limited number of cells.
Jab
A: 

Not sure exactly what you are trying to achieve with the images - but can I guess they are coming from a server and that is why you want to download them in another thread?

I would not try to load up the cells before you display the table - you should use lazy loading as much as possible to make sure you are making the most of the memory on a device.

My suggestion would be to look at using a subclass of NSOperation to manage the loading of images. Firstly NSOperation will handle all the complexity of threading for you and allow you to queue up the operations. You will then be able to prioritise the operations that you want completed for the cells at the top.

As each operation completes you can make a call back to the cell or tableViewController (perhaps create a delegate protocol to make this really easy).

If you have an operation per image/cell combination then you should be able to refresh each cell as the operation completes. Doing this along with prioritising the operations will give you an optimal solution.

If the NSOperations sound complex or you are put off by this - please do try it - it is a lot simpler than I might have made it sound.

Grouchal
A: 

Have you tried calling [cell setNeedsDisplay]?

drewh
OK, here's what I'm doing: from cellForRowAtIndexPath I'm sending set needs display to all visible cells. Yet, there's no diference and the cells are not updated. I'm starting to feel that setneedsdisplay is pretty much useles...
MihaiD
A: 

Have you tried calling [cell setNeedsDisplay] but on the main thread?

setNeedsDisplay when called on a background thread does pretty much nothing,

try this:

[cell performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
Aran Mulholland