Why numberOfSectionsInTableView is called twice in UITableViewController? - iphone

Is there any other reason (than calling "reloadData") why numberOfSectionsInTableView is called twice? I did debugging and found, that it's get called twice during initial startup when no custom reloadData statements are called.
I have created the table with IB but does that might cause a problem?

Have a look at the call stack. you can see that this method is being called from two different scenarios.

Probably your tableView object may instantiate twice. Once i have encountered the same problem due to this.

Related

Repercussions of Reassigning Delegates

I'm looking to find out what the effects are (if any) of assigning a delegate multiple times? Is it bad practice to assign a delegate inside viewWillAppear considering I do not assign the delegate to another viewController?
It is just a simple custom delegate for one of my classes. I need the delegate in one view of my tabBar, but not in the other. And since I've been assigning it in viewWillAppear, and viewWillAppear gets called every time you change tabs, I was just wondering if there were some unwanted effects.
No, in the case you describe there should be no problem. But it depends on what your code does when the delegate is assigned. If it's a simple property assignment with no custom implementation of the 'setter' method for the property, then you have no issue. But if you wrote your own 'setter' method for the property and the implementation performs other actions, then it is possible that those other actions could be an issue.
Either way, you may wish to consider moving the assignment to the viewDidLoad method. As if iOS 6 this will only be called once. Under iOS 5 or earlier it could be called more than once but viewDidUnload will be called as well in such cases.

How do I call my database class from my viewcontroller in Objective-C?

I'll try to explain as best I can:
I have two classes I'm currently focusing on. A Database Class and a View Controller, everything is currently set up like this:
View.h - Has "Database* myDatabase;"
View.m - Calls "OpenDB" during ViewLoad and has a button method that uses: [myDatabase getRow]; when clicked.
Database.h - Has "sqlite3 db;"
Database.m - Has "getRow" method and "openDB", which uses the "db" declared in Database.h.
This causes no errors during build, but when my program runs, when I click my button [myDatabase getRow]; does nothing. I used NSLog just inside the method and it never gets called. If it's hard to understand, I'll post code snippets, but I thought it would just be too much code.
Typically when you're not seeing a message that you're expecting to see, and the circumstances of it are completely baffling, it's because the receiver of the message is nil.
Put a breakpoint inside your viewDidLoad and make sure that myDatabase actually has a value assigned to it.
"po myDatabase" in gdb...
If in fact myDatabase is actually non-nil, then the second source of lots of grief trying to figure out why something's not happening the way you expect it to is missing connections in your XIB.
Make sure that your button's action is wired to your controller, or if you're programmatically setting the button's action, then make sure that your controller's outlet to the button is wired up.
You should create a object of Database class and access the methods using those objects.
Another workaround is to change those methods to static methods.

UIAnimator message sent to deallocated instance

I am getting crazy:
*** -[UIAnimator removeAnimationsForTarget:]: message sent to deallocated instance 0x5ba13d0
It happens in different moments, when I scroll my tableview, when I switch my filter (a UISegmentedControl).
How can I fix it?
I just solved the same problem.
I thought it was related to UIAnimation, but it was related to UITableViewCell, instead.
I found a good starting point looking at this article.
http://marius.me.uk/blog/2011/mar/fixing-uianimator-removeanimationsfortarget-crash/
Good luck and let me know!
I had the same symptom (crash caused by [UIAnimator removeAnimationsForTarget:] message being sent to deallocated UITableViewCell, but it turned out to have been caused by a reason different from the one cited in the above solution. It turned out that the reason was that my UI was being updated by a non-UI thread. Specifically, I was calling popViewController:animated: from a background thread. When I moved this invocation to the UI thread via a callback the problem went away.
Both #er0 and #notme's answers are right.
When I made two different Cell UI in storyBoard and tried to use them in tableView:cellForRowAtIndexPath it gave me this error on a completely different button action. That method is not related to UITableViewCell in any way (AFAIK).
First I used #er0's way: on the method I was getting the crash, performed it in main thread.
performSelectorOnMainThread:withObject:waitUntilDone
It solved the crash.
Then I came to realize my tableView:cellForRowAtIndexPath code needs to be refactored. In some special situation I was using dequeueReusableCellWithIdentifier two times for a single indexPath. Refactored the code and placed the if-else in a way that dequeueReusableCellWithIdentifier is not called twice for a single indexPath. It solved my crash and I don't need the performSelectorOnMainThread" any more.
I ended up using notme's solution.

cellForRowAtIndexPath called before viewWillAppear iPhone SDK 3.2 plus

After upgrading my SDK version I noticed that cellForRowAtIndexPath is always called prior to viewWillAppear. Previously the order of execution was opposite: viewWillAppear would always be called prior to cellForRowAtIndexPath. Because my application logic often used viewWillAppear to initialize objects that are subsequently used in cellForRowAtIndexPath, the application is often crashing.
I tried searching for an official expiation regarding this change with no success. I can likely move my initialization code to viewDidLoad, but wanted to see if there are better solutions or more information about this change in behavior.
You can just add the line
[self.tableView reloadData];
at the end of viewWillAppear and your problem will be fix
It is not "cellForRowAtIndexPath called before viewWillAppear", it is [super viewWillAppear:animated] will call UITableView's delegate.
I made the same mistake and it took me a lot of time to find it.

Trick to getting initWithNibFile to be invoked?

I've implemented a View Controller, and I'm trying to do some very basic initialization in initWithNibFile...which I understand to be the designated initializer for View Controller objects.
I've tried to put breakpoints on the code, and have put very simple NSLog statements into it as well. Regardless...I'm not seeing it be executed (and the object i'm attempting to alloc/init inside the function is also not being allocated - so I'm about 99% sure I'm not hitting the code.
Is there something I need to do elsewhere to cause this method to be invoked?
I'm getting a clean build, no warnings or errors. And the app successfully loads up the View, and I can invoke a ButtonClick method I've coded and connected to this same View Controller.
Any suggestions would be appreciated.
TC
I ended up moving my allocation logic to viewDidLoad, and that works fine.
Not sure why the initWithNibFile was not working...but I'll take the small victory !!!
Thanks for offering to look at the code bpapa.
You probably need initWithCoder: It's what the SDK uses to read from nib files during startup.
initWithNibFile: is almost never called by the system. Usually you call this manually. The documentation is quite misleading on this point.
In any case, be careful doing too much initialization in the initWithXXXX methods. The view's targets and actions aren't likely to be set up and connected yet. viewDidLoad is almost always the right place to do viewController setup anyways.