Question

I have an NSTableView and an array controller set up as shown here, using cocoa bindings: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/PopulatingViewTablesWithBindings/PopulatingView-TablesWithBindings.html#//apple_ref/doc/uid/10000026i-CH13-SW3

In my app delegate during applicationDidFinishLaunching I have the following snippet in here, initialising the array and filling it with objects

array = [[NSMutableArray alloc] init];

SomeObject* foo = [[Object alloc] init];
foo.text = @"sup";
[array addObject:foo]; //Repeat this a few times

However, when I build the app and run it I end up with an empty table. However, if I bind a button to the array controller's add: input and click it during runtime (this adds a new object to the array and table) then the table will show the new object first, with the objects added during applicationDidFinishLaunching following it.

Why does this happen? And is there a way to make my table populate without having to add an element first?

Was it helpful?

Solution 2

As it turned out, I should have:

  1. Linked the array controller as a property to my app delegate
  2. Added the objects to the array controller itself

This was in my app delegate.h:

@property (copy) NSMutableArray *pastes;
@property (assign) IBOutlet NSArrayController *controller;

and my app delegate.m:

@synthesize pastes, controller;

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    pastes = [[NSMutableArray alloc] init];

    [controller addText:@"sup"];
    [controller addText:@"hey"];
    [controller addText:@"hi"];
}

OTHER TIPS

NSArrayController does not track changes to mutable arrays, just changes to the array property it is observing. So what you want to do instead is:

NSMutableArray  *mutableArray = [[NSMutableArray alloc] init];

SomeObject* foo = [[Object alloc] init];
foo.text = @"sup";
[mutableArray addObject:foo]; //Repeat this a few times

array=mutableArray;

The array controller will see array being changed to the populated array.

I hope you have already bind your table column to array controller. So just you have to take dictionary and then set that value to the key of table column and then add that dictionary to the mutable array and then set your mutable array. And also include this code on your windowDidLoad or awakeFromNib method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top