Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

macos

cocoa

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.

like image 592
Andy Avatar asked Oct 11 '08 15:10

Andy


1 Answers

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]
}
like image 52
Milly Avatar answered Sep 21 '22 05:09

Milly