NSMutableArray empties after tableView editing turned on - nsmutablearray

Very simple situation... very strange problem.
I have a retained iVar NSMutableArray that I load a few objects in on viewDidLoad.
That array is used to populate a tableView.
Everything is fine... until
I turn editing on for the tableView.
Suddenly the array is empty!
Ideas?

You may need to initialize the NSMutableArray in the init method. That's the most common problem that I've noticed- adding objects to an array that hasn't been created doesn't let you access the objects later.

Related

IBOutlet inside document based application

I've created an application document based but I had problem to handle IBOutlet becouse when I selected some controller inside the document in the first window sometime the other same controller in another window document was selected too...
Maybe I did wrong with strong/weak/readwrite or something I'm not very sure so I've decided to force my application to have just one window document for time.
Now my problem. I've an arrayController and when I launch my application and I do open saved file it close the "blank document" created by default and show the new window document with the data loaded.
The problem is that the arrayController of the second window called inside the windowControllerDidLoadNib is the right pointer (created by makeWindowControllers) but when I use this inside the code it has another pointer that is the old arrayController of the blank document that is been closed!! Why? Do close method release everything? (I use ARC).
Excuse me if my words are confused, I'm newbie. Thank you.
This is my code:
Document* newDoc = [[Document alloc] init];
[[NSDocumentController sharedDocumentController]addDocument:newDoc];
[newDoc makeWindowControllers];
[newDoc showWindows];
I've solved this issue: when you define notification for some object as a nsdocument these are not released after close calls. So you need to remove observer!

create a global NSMutableArray

I have a lot of View Controller and all use the same Class (ViewController).
In ViewController.m I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
pedidoNomeArray = [[NSMutableArray alloc] init];
pedidoValorArray = [[NSMutableArray alloc] init];
}
and when I go to View Controller number 2 my array pedidoNomeArray and pedidoValorArray are null.
What I have to do to use the same array in all my View Controllers?
thanks
One reason could be that you are not using ARC. In this case you assign the newly initialized array directly to an ivar, i.e. without using a setter. In this case, the array is not retained, and released as soon as your app enters the run loop. So, if you are not using ARC, use pedidoNomeArray = [[[NSMutableArray alloc] init] retain]; instead. But you should really use ARC since it avoids most of the common memory management problems.
EDIT (due to you comment):
Now it seems to me that what you need is a data model according to the MVC software architecture pattern. Your data are stored in an independent object, and both of your view controllers have a reference to this object. Your data model, which contains your "global array" could be instantiated e.g. in your application delegate or your root view controller, this is debatable, examples and discussion see here. I hope this helps.

UITableView not loading data the second time

I have a UITableView and I call reloadData in the viewWillAppear method
- (void)viewWillAppear:(BOOL)animated {
[tableView reloadData];
}
However, the second time my view appears, neither numberOfRowsInSelection nor cellForRowAtIndexPath are called
What could I be doing wrong?
I am seeing the same problem and yes, using a breakpoint I have already confirmed that viewWillAppear is called and reloadData executed - but cellForRowAtIndexPath is never called and the table contains stale data.
New information: I tried creating a new class/nib, this time using a UITableViewController instead of a UIViewController and implementing the delegate & datasource interfaces manually. This works as expected - cellForRowAtIndexPath is called and the table is updated. There must be something else UITableViewController does for me that I need to implement in my version, but I don't know what it is....
Solved! My issue, anyhow. I had wired up the datasource and controller connections, but hadn't connected the tableView outlet in my class to the table in the UI builder. smacks forehead. Mine works now, hope this helps!

How can I suspend the working of an NSFetchedResultsController?

I have a UITableViewController fed by an NSFetchedResultsController. From it, the user can call up a modal ViewController in which he or she can enter new data. As this begins, I create a temporary object as follows:
newPtr = [[Entry alloc] initWithEntity:[NSEntityDescription
entityForName:#"Entry" inManagedObjectContext:self.nmocontext]
insertIntoManagedObjectContext:self.nmocontext];
As the user makes choices, attributes of this 'provisional' object, newPtr, are set.
The problem is that the base UITableViewController remains active while the modal ViewController is visible. It seems to be freaking out (causing crashes) in some cases when it realizes a mandatory attribute of newPtr has not been set yet.
What can I do to stop the NSFetchedResultsController from looking at my managed object context until the modal ViewController is dismissed?
Core Data supports "nested" managed object contexts which allow for a flexible architecture that make it easy to support independent, cancellable, change sets. With a child context, you can allow the user to make a set of changes to managed objects that can then either be committed wholesale to the parent (and ultimately saved to the store) as a single transaction, or discarded. If all parts of the application simply retrieve the same context from, say, an application delegate, it makes this behavior difficult or impossible to support.
I haven't tested this myself but a possible approach would be to implement viewWillAppear and viewWillDisappear, and set the fetchedResultsController delegate to self on will appear and nil on will disappear.
OR
You could create an NSObject that mirrors the attributes of your NSManagedObject in your editing window. Once the user has finished editing the attributes (and you have run the appropriate validation rules) you can pass them back to your NSManagedObject instance and let the fetchedResultsController do its job.

tableview methods are been called only Once in Objective C on iphones?

i have tableview.In method 'viewWillAppear',it calls a function and function data is stored in an array named 'list'.Now in the method 'numberOfRowsInSection' method it returns the list count...and in another method 'cellForRowAtInexPath' it displays data in cell...now when the view is pushed to the above view using pushViewnavigationcontroller,function is called in viewwillappear and data is shown in tableview..now when i come back to previous view and move to tableview view again...viewWillAppear method is called(i checked using NSLog)...but tableview displays the same previous data..that means methods numberOfRowsInSection,cellForRowAtIndexPath are called only Once at first click..why is it so??and what can be done to load the tableview again and again with changing values of the data..??
I found the solution by myself. Just use [tableview reloadData] in method viewWillAppear.

Resources