Why UITableView methods are being called before viewWillAppear? - iphone

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.

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.
User pushed button
You fire a method on the destination view, gathering information
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.

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.

Related

When to put into viewWillAppear and when to put into viewDidLoad?

I get used to put either of viewWillAppear and viewDidLoad, it's OK until know. However I'm thinking there should be some rules that guide when to put into viewWillAppear and when to put into viewDidLoad?
Simple rule I use is this. viewDidLoad is when the view's resources are loaded. The view is not drawn onscreen yet. So calculations and code dealing with the geometry and visuals of the view should not be put here. They should be in the viewWillAppear or viewDidAppear method.
Also viewWillAppear can be called multiple times
When a popover/modal view is displayed and remove
When an alert view/actionsheet/uiactivityController's view is displayed and removed.
For these reason, viewWillAppear should not contain codes that takes longer to finish. (at least the code running on the main thread). Neither should codes that only needs to be run once per view display.
There are more I am sure but these are simple to remember and I hope it helps.
viewDidLoad: Alerts you that a view has finished loading
viewWillAppear: Runs just before the view loads
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.

Is it possible to call a UIEvent between viewWillAppear and viewDidAppear?

Is it possible to handle UIEvents between viewWillAppear and viewWillDisappear?
For example:
Let's say I have a view controller with a UITableView.
When pressing a cell I push a another view controller to the navigation controller (And display it of course).
Is it possible that after the viewWillAppear event of the pushed view (And before the viewDidAppear) I can still press one of the cells in the UITableView?
I am asking this because I am seeing this in the logs of my application and I Never thought that such a thing could be possible..
What do you guys(Or girls) think?
Thanks!
I have one idea may this work,
you use performSelector in viewWillAppear with 0.01 or another time with Delay Parameter and call your method here,
hope,this help you.
:)

iphone - numberOfRowsInSection being called before viewDidAppear

This might be normal, but I'm new to objective-c.
I have an app with two view controllers, both are table views.
When a row is clicked, I load the second view to the top of the stack and pass a parameter through one of it's properties.
On the first view (loaded by default by the navigation controller) the viewDidAppear function is called first, it opens an XML file from a URL, builds an object for each node and pushes all the objects in to an array. After this is done, numberOfRowsInSection is called and it returns the count for the array and all is well.
On the other view, after it is loaded first the numberOfRowsInSection is called and then viewDidAppear. I'm really new to the iphone and I have no idea why this is even happening.
Thank you!
This is normal. viewDidAppear is called after the entire view is has finished loading and became visible.
This includes the UITableView. The Table View in turn requires the numberOfRowsInSection function to initialize.
You have three options to execute code before the UITableView loads:
1) Place your code in -viewWillAppear - this can occasionally be shaky
2) Subclass the UIViewController (chances are you are already doing this) and create a custom -init function to handle your setup
3) Put your initialization code inside of -numberofRowsInSection. This is always the first method of the UITableViewDataSource to be called. It is hackish but works very well in practice.
Maybe you should consider moving your code to -viewWillAppear or -viewDidLoad .
Thank you all for taking the time to reply!
I found the issue.
Being new to xcode/iphone/mvc, I forgot to connect the view to the IBOutlet object.
A few hours of my life I'll never get back.

with "viewDidLoad" my viewController take much time to appear!

when i load my viewController i used "viewDidLoad"method to init my view ,but this take much time to make the view appeared .So i had the idea to use "viewDidAppear" method to accelerate the appearance of my view but the load of the informations about my view are now loaded to the memory every time that i push my view (which is normal) or i pop to it(and there is my problem)
Have you an idea?
Create a background task that is started in viewDidLoad and just updates the GUI when it's finished. This should at least let you show the GUI, but possibly without valid data.
According to the View Controller Programming Guide, you are supposed to create your view in the loadView method :
If you prefer to create views programmatically, instead of using a nib file, you do so from your view controller’s loadView method. You must override this method if you plan to create your views programmatically.
Maybe you should create your view in the loadView method and then load extra data in the viewDidLoad as explained in Understanding the View Management Cycle, using a background task as suggested by willcodejavaforfood if necessary.
i did it with the method "ViewDidAppear:animated" and for the problem of loading data for every appearance i deal with it with a test on the top of the mehod:
if(data==nil){/*i do ...*/}
and for me data was an array that i am writing on when loading the view

UITableView cellForRowAtIndexPath occasionally not called?

I'm developing a graphing application that on the main navigation/tab view displays a UIView that renders a graph using openGL. Beneath that view is a UITableView that displays the list of elements on the graph, and an add button.
Occasionally, when the user clicks on another tab, and then returns to the graph view tab, the table view does not get redrawn.
I have a [tableView reloadData] method being called in the navigation controllers' (also the table view's delegate and data source) viewDidAppear method.
numberOfSectionsInTableView and numberOfRowsInSection get called, but cellForRowAtIndexPath does not despite both latter methods returning positive values.
This is an intermittant problem, only happening some of the time, but it's not clear what (if anything) influences this.
Does anyone have any ideas?
[edit] Should just quickly add; clicking or otherwise interacting with the table view, whether faulted as described or not, ALWAYS causes it to be sucessfully redrawn.
[And again] A bit more information; when it faults as described, and calls the first two but not the cellforRowetc method, it seems to wait until the user interacts with the table before cellForRowetc is finally called (the first two methods are then not called) and is redrawn.
I had a UIActivityIndicator, started on a seperate thread, that showed when requests were being made to a web service. When the web service calls were completed, a notification was sent to the graph controller class to stop and hide the UIActivityIndicator. In this method, reloadData was being called on the table whether the view was visible or not, which seemed to be causing the problem.
The conclusion seems to be not to call reloadData on a table unless it's visible.