views:

760

answers:

2

Hi, I was looking forward to find a cause for my application being crushed at some point and I found out that methods of UITableView are being called before or at the same time as viewWillAppear is called.

In viewWillAppear I have code that is initializing number of rows in each section but numberOfRowsInSection is being called before I finished setting up array that has amount of rows in each section.

I believe that viewDidLoad is not suitable in my case because it is being called only once after launching an application. Am I right? And I need to make my initialization function called each time a view appears on the screen.

How can I overcome this failure?

Thank you in advance.

+1  A: 

Well, I think this 'problem' has to do with that there are multiple threads running taking care of the view and the UITableView.

The view calling this view could (before switching to this view) call a method on the View which gathers the data.

  1. User pushed button
  2. You fire a method on the destination view, gathering information
  3. Switch to view

You could work with delegates to know when the destination view is ready loading the data you needed, so you can switch to that view then.

Hope this helps.

Wim Haanstra
A: 

I've just had this same issue, from tab 0 of my application I sent some data across to tab 1 which has a tableView. Then in viewWillAppear I tried to do this:

[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

With the result being that the first time this was done the new controller that was pushed went mad. Navigation was wrong and there was no data. I've now gone and used a timer in viewWillAppear:

timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(selectItem) userInfo:nil repeats:NO];

which calls selectItem which does the didSelectRowAtIndexPath. All works fine but it feels very wrong. How would I use the delegates to find out from the view when it's ready to do my bidding?

Alistair