views:

19

answers:

1

I have a UITableView that changes its data very often. (It's just NSStrings, stored in an NSArray.)

Method 1> [self loadMyArray];
Method 2> [myTable reloadData];

Where would I put those 2 methods... so they get called EVERY time the view is displayed?

I assume some likely places would be:

viewWillLoad
viewDidLoad
viewWillAppear
viewDidAppear

But I can't seem to find the placement that is the one I need: Each time the view appears... but BEFORE the UITableView builds itself.

If I put my 2 methods inside either "Load" delegate... it doesn't get refreshed often enough.

If I put my 2 methods inside either "Appear" delegate... I notice the data is loaded twice. (Wasteful)

A: 

The short answer is that 99% of the time, you should always put [myTableView reloadData] in viewDidLoad.

If you would like the UITableView to reload its data when you return to the UIView from a child view (e.g. you updated some of the data), then you need to consider what it is that you are using in the UITableView. Most of the time, I am using an NSFetchedResultsController which has its own methods to handle data updates. However there are some times when I need to update other kinds of data when the UITableView is reloaded. Here's how I deal with it: Instead of putting [myTableView reloadData] in viewDidLoad, I put it in viewWillAppear.

If for some reason the table is taking a long time to load and it shows a blank table before showing your data, I would look into profiling your data loading functions using Instruments, perhaps the real issue is that you need to optimize the function so that it takes less time.

Jason