NSArrayController & NSTableView accessing different arrays of data?

Discussion in 'Mac Programming' started by peterf303, May 31, 2010.

  1. peterf303 macrumors newbie

    May 31, 2010

    I have an NSTableView and NSArrayController working with an NSMutableArray just fine but i'd like the TableView to display/edit different sets of data depending on a setting in my code.

    This is what I have right now, with the TableView working with the data in currentGroupArray.
    NSMutableArray *groupArrayOne;
    NSMutableArray *groupArrayTwo;
    NSMutableArray *groupArrayThree;
    NSMutableArray *currentGroupArray;
    IBOutlet NSTableView *groupTableView;
    IBOutlet NSArrayController *groupController;
    I would like to be able to chage the TableView change to use one of the other three NSMutableArrays programatically.

    I've tried just assigning currentGroupArray to one of the other three, but that breaks the model of only having the ArrayController access the MutableArray (I think).

    I've also tried making the Model Key Path of the ArrayController call a getter method for currentGroupArray that selects one of the other three arrays, but this didn't work at all.

    I can think of two other ways to do this but I'm hoping there's a better way.

    1) I can swap objects in & out of the currentGroupArray using the ArrayController's addObject everytime I change which array to display but this seems clumsy.


    2) Maybe have a seperate NSArrayController for each of the three NSMutableArrays?

    What's the correct way to go about this?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    This is probably the best way to do it, but you need to use Key-Value Coding when assigning the array otherwise your array controller won't update.

    Either implement a standard setter setCurrentGroupArray: or use @properties to assign the value of currentGroupArray to one of your groupArrayOne/Two/Three objects.
  3. peterf303 thread starter macrumors newbie

    May 31, 2010
    Hi, You put me on the right path with Key-Value coding. I changed my code to use the setValue:ForKey message to change the value of currentGroupArray and it all works perfectly now. Thanks a lot, this has made a lot of things much clearer to me about Cocoa programming :)

Share This Page