Ilya Suzdalnitski
Ilya Suzdalnitski

Reputation: 53560

Why UITableView methods are being called before viewWillAppear?

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.

Upvotes: 1

Views: 2333

Answers (2)

ChenXin
ChenXin

Reputation: 373

I think the right answer is that "[super viewWillAppear:animated]" will call UITableView's method.So it is necessary to put this code after the code that initialize the tableView.

Upvotes: -1

Wim Haanstra
Wim Haanstra

Reputation: 5998

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.

Upvotes: 2

Related Questions