David Beck's Blog

NSTableView, NSArrayController and editing new rows

Sunday, May 10th, 2009

Figured out a very nice solution to automatically edit new rows added by NSArrayControllers via bindings! I searched for a while and didn’t find any correct solutions, so I made my own.

The problem is that any catch that you would normally use, including subclassing, wont work because NSArrayController defers changes until the next run loop.

I have a table which my object is the delegate of. The table is bound to the array controller. The array of the array controller is filled with NSMutableDictionarys.

So I have

Here’s what I did:

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
	if([[[data objectAtIndex:[table selectedRow]] allKeys] count] == 0)
		[table editColumn:0 row:[table selectedRow] withEvent:nil select:YES];
}

First we become the delegate of the table and implement the tableViewSelectionDidChange: method which is sent after the selection is changed. Then we check if the object (an NSMutableDictionary) has any keys set. If no keys are set, we assume that the row is new. This is different than if the keys were set to empty strings. editColumn:row:withEvent:select: scrolls to the row and column and starts editing the cell.

Filed under: Programming Tags: , ,

2 Responses to “NSTableView, NSArrayController and editing new rows”

  1. This assumes you have access to your data (ivar data in your code). What if you don’t?

    A way to do it is by grabbing the NSArrayController (think -infoForBinding:) bound to the table column and also the model key path for said binding. Once you have that then you can get the data for the appropriate row via KVC’s -valueForKeyPath: where the path will be something like selection.

    Cheers

  2. Hmm.. it didn’t like the less than/greater than brackets, so I continue my last sentence:

    …something like selection.(whatever is returned by the binding info when you asked for the @”value” binding).

Leave a Reply