Question

I have a UITableView subclass and a UITableViewCell subclass 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?

Was it helpful?

Solution

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];

OTHER TIPS

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.

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.

Have you tried calling [cell setNeedsDisplay]?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top