Andy
Andy

Reputation: 30418

How can I get notified when the user finishes editing a cell in an NSTableView?

I need to know when the user finishes editing a cell in an NSTableView. The table contains all of the user's calendars (obtained from the CalCalendarStore), so in order for the user's changes to be saved I need to inform the CalCalendarStore of the changes. However, I can't find anything that gets called after the user finishes their editing - I would guess that there would be a method in the table's delegate, but I only saw one that gets called when editing starts, not when editing ends.

Upvotes: 5

Views: 5701

Answers (6)

J.beenie
J.beenie

Reputation: 831

Translating @Milly's answer into Swift 3:

// Setup editing completion notifications
NotificationCenter.default.addObserver(self, selector: #selector(editingDidEnd(_:)), name: NSNotification.Name.NSControlTextDidEndEditing, object: nil)

Function to handle notification:

func editingDidEnd(_ obj: Notification) {
    guard let newName = (obj.object as? NSTextField)?.stringValue else {
        return
    }

    // post editing logic goes here
}

Upvotes: 0

Milly
Milly

Reputation: 725

You can achieve the same result without subclassing NSTableView by using NSNotificationCenter or using the NSControl methods. See the Apple documentation here:

http://developer.apple.com/library/mac/#qa/qa1551/_index.html

It's only a couple of lines of code and worked perfectly for me.


If you can be the delegate of the NSTableView you just need to implement the method

- (void)controlTextDidEndEditing:(NSNotification *)obj { ... }

In fact, NSTableView is the delegate of the NSControl elements it contains, and forwards those method calls to its delegate (There are other methods that are useful)

Otherwise, use the NSNotificationCenter:

// where you instantiate the table view
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd:)
    name:NSControlTextDidEndEditingNotification object:nil];

// somewhere else in the .m file
- (void)editingDidEnd:(NSNotification *)notification { ... }

// remove the observer in the dealloc
- (void)dealloc {
   [[NSNotificationCenter defaultCenter] removeObserver:self
    name:NSControlTextDidEndEditingNotification object:nil];
   [super dealloc]
}

Upvotes: 15

Nathan Kinsinger
Nathan Kinsinger

Reputation: 25041

Subclass NSArrayController and override objectDidEndEditing: (be sure to call super's implementation).

This will mostly only be invoked by text fields NSTextFieldCell or NSComboBoxCell (but only when changing the value by typing it, not by selecting the value from the combo's menu). There may be a few other cells that will invoke it, but I'm not sure which ones. If you have a custom cell then consider implementing the NSEditor and NSEditorRegistration informal protocols.

Upvotes: -1

Nathan Kinsinger
Nathan Kinsinger

Reputation: 25041

Subclass NSTableView and override textDidEndEditing: (be sure to call super's implementation).

This will only be invoked by text fields NSTextFieldCell or NSComboBoxCell (but only when changing the value by typing it, not by selecting the value from the combo's menu).

Upvotes: 2

Nathan Kinsinger
Nathan Kinsinger

Reputation: 25041

Set up observers for each item in the content array using addObserver:toObjectsAtIndexes:forKeyPath:options:context:

You will also need to set an observer for the array itself, so that you will be notified about objects that are added to or removed from the array.

For an example look at the iSpend project.

Upvotes: 2

Lounges
Lounges

Reputation: 4664

Look into the NSTableDataSource protocol. The message you are looking for is called: tableView:setObjectValue:forTableColumn:row:

Upvotes: 1

Related Questions