NSTableView + Delete Key

Leandro picture Leandro · Jan 12, 2011 · Viewed 8k times · Source

I'm looking for an easy solution to delete NSTableView rows by pushing the delete key.

All I have seen when searching in Google were answers like this: http://likethought.com/lockfocus/2008/04/a-slightly-improved-nstableview/

This seems to me an Engineering solution, but I would like to know if this is the best way. Does any one know a better answer?

Answer

Craig Otis picture Craig Otis · Jan 12, 2011

What I usually do is create a new menu item in your application's menu bar. Something like:

File -> Delete ${Name of Item}

Then you can link that NSMenuItem in Interface Builder to point to an IBAction method defined somewhere on either your app delegate or some other controller. The implementation for this method should delete the item from your model, and refresh the NSTableView.

The advantage to making an NSMenuItem out of the action is that:

  1. You can give the item a keyboard shortcut in Interface Builder. (Like the delete key.)
  2. Users who are not familiar with your application, afraid to press the delete key, or do not have access to a keyboard for whatever reason, can still make use of this functionality.