Tony Giaccone
Tony Giaccone

Reputation: 511

NSTable CoreData and selection

I have a very simple App. It is a non-document based CoreData app. The nib contains one NSArrayController and a window with a NSTable with one column. Below the table are three buttons, Add, Delete, and Fetch. Below the buttons I have one NSTextField and a fourth button labeled Save.

My model has two Entities, Department an Employee. The Department has one attribute, name. I'm not currently using Employee.

In the AppDelegate.h I have added this code:

@interface HMGAppDelegate : NSObject <NSApplicationDelegate>
{
    NSArrayController *_departments;
}

@property (retain) IBOutlet NSArrayController *departments;

and in the AppDelegate.m

 - (void) awakeFromNib
{
    [[self departments] fetch:nil];
}

The buttons are bound to add: remove: and fetch: on the NSArrayController. The save button is bound to save: on the AppDelegate

The NSTextField is bound to the instance of the NSArrayController in the nib, ControllerKey is selection, Model Key Path is: name

When I run the app, I can create new entities and change the value of the entities. That all works fine.

What doesn't work is selection in the table. Changing the selected row in the table does not cause the value from that row to be selected in the NSArrayController. I understand I could put a delegate on the table catch SelectionDidChange and explicitly set the selection on the ArrayController, but I wonder if there is an easier way to do this.

I have seen other examples where there are two table views, representing a one to many relationship. Selection in the first table causes the second table to display the dependent entities. (for example choosing a department shows the employees of that department). In the example I'm looking at there doesn't seem to be a NSTableDelegate.

What am I missing? How does the NSTableView communicate its selection back to the NSArrayController?

Upvotes: 0

Views: 259

Answers (1)

florianbuerger
florianbuerger

Reputation: 422

How did you setup the Binding? If you bind NSTableView's content to NSArrayController, you have to bind the selection & sortDescriptions as well.

As an alternative, you can bind the table column's Value Binding to Array Controller.arrangedObjects and the selection should work.

Upvotes: 1

Related Questions