Issue with NSTableView and NSArrayController

Discussion in 'Mac Programming' started by stadidas, Nov 2, 2007.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    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.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #3
    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.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Just setup an IBOutlet for the NSArrayController, and then you can access it directly.

    Edit: it might be better to use addObject: or addObjects:
     
  5. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #5
    Ah of course, it seems so obvious now. Thanks very much, I shall give it a whirl when I get home from work.
     

Share This Page