The right approach to loading dynamic content into a UITableView in iOS

Posted by OS. on Stack Overflow See other posts from Stack Overflow or by OS.
Published on 2012-04-03T21:08:31Z Indexed on 2012/04/03 23:29 UTC
Read the original article Hit count: 170

Filed under:
|
|
|

ok, I've read tons of bits and pieces on the subject of loading dynamic content (from the web) into a UITableView and the problem with calculating cell height upfront. I've tried different simple implementations but the problem persists...

Assuming I need to read a JSON file from the web, parse it into 'item' objects, each with variable size image and various text labels, here is what I believe would be the right approach to avoid long hang time of the app while everything is loading:

  1. on app load read JSON file and parse into items array
  2. provide only small part of the items array to the tableview (about 10 items) - since I need to load the images associated with each item to calculate cell height - I don't want the view to go through the whole items list and load all images - this hangs the app until every image is loaded
  3. display the tableview with the available cells (assuming I load a few 'spare' ones, user can even scroll to more items)
  4. in the background using Grand Central Dispatch download images for all/some of the remaining items and then reload the tableview with the new data (repeat step 4 if item list is very long)

Step 2 above is necessary since I have no way to calculate the cell height without loading the images first, and since tableview first calculates height of all cells it may take a very long time to download all images for all items.

Would you say this is the right approach? am I missing something?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c