Technical Q&A QA1551

Detecting the start and end edit sessions of a cell in NSTableView.

Q:  How do I detect start and end edit sessions of a cell in NSTableView?

A: How do I detect start and end edit sessions of a cell in NSTableView?

In order to detect when a user is about to start and end an edit session of a cell in NSTableView, you need to be set as the delegate of that table and implement the following NSControl delegate methods:

Listing 1  NSControl delegate methods

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor; - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor;

The table forwards the delegate message it is getting from the text view on to your delegate object using the control:textShouldEndEditing: method. This way your delegate can be informed of which control the text view field editor is acting on its behalf.

To further detect when editing has completed, you need to register for NSControlTextDidEndEditingNotification, and implement a method to catch that notification:

Listing 2  NSControlTextDidEndEditingNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd:)
        name:NSControlTextDidEndEditingNotification object:nil];

- (void)editingDidEnd:(NSNotification *)notification
    // perform your work here

Try not to equate this with the NSTableView text delegate methods:

Listing 3  NSTableView: Text delegate methods

- (BOOL)textShouldBeginEditing:(NSText *)textObject; - (BOOL)textShouldEndEditing:(NSText *)textObject;

In order to implement these, you need to be a subclass of NSTableView. When the user edits a table cell the table puts an NSTextView in place of the cell in order to actually perform the editing. This is called the window's "field editor".

The NSTableView configures itself as the delegate for the NSTextView so that the table gets notified when editing begins and ends. Hence, if you are a subclass of NSTableView you can then implement any or all of the text delegate methods described in the NSTextView documentation.

Document Revision History


New document that describes the proper way to control and detect edit sessions of NSTableView using NSControl's delegate methods and notifications.