Historically, adding editing support to cell-based NSTableViews has been as easy as implementing NSTableViewDataSource’s -tableView:setObjectValue:forTableColumn:row: method. Double-clicking a text cell would make it editable and the aforementioned data source method would be called with the updated value.
View-based table view cells, however, use standard NSTextFields which, out-of-the-box, don’t have support for the double-click-to-edit behavior that users expect of tables on OS X.
To implement that behavior, we have to subclass NSTextField and override -mouseDown: to handle double-clicks and make the text field editable. Below is a sample implementation that takes care of the basics; it’s actually very similar to Pixen’s implementation of the text field used to display (and edit) layer names.
If you change your text field’s class to EditableTextField and try this out, you’ll notice -mouseDown: is actually never called when you double-click the text field. It appears as if NSTableView is eating up the mouse down events to handle row selection (among other things). If you search around the web, the most common solution is to override -mouseDown: on the table view itself and forward events to the text field manually.
There’s a better way. Buried deep inside NSResponder.h is a hidden gem:
Instead of overriding -mouseDown: on NSTableView, we merely have to implement -validateProposedFirstResponder:forEvent: such that it returns YES for the text field we wish to edit. This will let -mouseDown: events propagate to the text field rather than stop at the table view.
Here’s a simple implementation that does just what we need:
Sure enough, double-clicking the text field now makes it editable, and hitting the Return key turns it back into a label.