PDA

View Full Version : Issue with NSTableView and NSArrayController




stadidas
Nov 2, 2007, 05:00 AM
Hi all,

I have been writing an application in which I have an NSArrayController binding an instance of NSMutableArray to a NSTableView. When I insert objects using the insert: action of the array controller, a new object appears in the table view as expected. However, I have another method which adds objects directly to the mutable array. The problem is when I do this the table view does not update until the insert: method is called again. I assume this is because I am bypassing the bindings layer.
Is there any way of accessing the array controllerís insert: method programmatically? If not, how can I have the table view refresh itself when a new object is added to the mutable array?

Thanks for any help you can give, if you need any more detail just ask.



kainjow
Nov 2, 2007, 10:07 AM
Use the array controller's add: method to add objects to it directly, not to the array. If you setAutomaticallyPreparesContent:YES (can be enabled in IB), then you don't even need to manage an array - you just have to work with the array controller, and it'll update automatically.

stadidas
Nov 2, 2007, 10:12 AM
Use the array controller's add: method to add objects to it directly, not to the array. If you setAutomaticallyPreparesContent:YES (can be enabled in IB), then you don't even need to manage an array - you just have to work with the array controller, and it'll update automatically.

Thanks kainjow, that sounds great.
How do I access the array controller from my document class? At the moment I was using the insert: method by attaching a button in IB (but this isn't how I want to do it, it was just to check it worked). Once I can access the array controller from code I will be able to implement your solution.

kainjow
Nov 2, 2007, 10:16 AM
Just setup an IBOutlet for the NSArrayController, and then you can access it directly.

Edit: it might be better to use addObject: or addObjects:

stadidas
Nov 2, 2007, 11:33 AM
Just setup an IBOutlet for the NSArrayController, and then you can access it directly.

Edit: it might be better to use addObject: or addObjects:

Ah of course, it seems so obvious now. Thanks very much, I shall give it a whirl when I get home from work.