Joran
Joran

Reputation: 69

NSTableView bindings how to add a row

I'm working on a application with the this interface (sorry the language is Dutch):

http://www.flickr.com/photos/pluueer/5756159100/

The Add function (incl. the four NSTextFields) under the NSTableView are to moved to a sheet someday, but for now this is fine. I've set up bindings according to a tutorial (http://cocoadevcentral.com/articles/000080.php), but the tutorial doesn't supply how to add rows in the way I want to (just adds an empty row which you need to edit in the NSTableView).

I've got a connection between the 'Voeg toe' (Dutch for 'Add') button and the Array Controller. But after clicking I get the message:

2011-05-28 23:37:56.149 Hop Calc[4345:a0f] -[__NSPlaceholderDictionary initWithObjects:forKeys:]: number of objects (0) not equal to number of keys (4)

It makes sense, because I've not implemented anything for adding rows, but I just don't know how.

Upvotes: 3

Views: 1666

Answers (1)

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

"Add a row to the table" is the wrong way to think of it. Your table represents a collection and a controller provides the information to the table, mediating between the table (view) and the collection (model). Since you mentioned bindings, the collection is likely managed by an NSArrayController. So you want to add a new object (of the kind your array controller manages) to the array controller's content array.

Simplest way: Connect the Add button to the -add: action of the NSArrayController. It'll add an empty row.

If you want more control, connect the Add button to your own custom action in some controller. That action will create an instance of whatever's represented by your array controller, prepopulate it (or whatever you want to do), then, using an outlet it holds to your NSArrayController, will call the array controller's -addObject: method to add the object (the possibly a -rearrangeObjects call to get the array controller to re-sort its contents).

Upvotes: 5

Related Questions