rentzsch.com: tales from the red shed

NSTableView+ CocoaBindingsDeleteKey

Cocoa

You know how it’s easy to use Cocoa Bindings to hook an NSTableView to an NSArrayController? You know how it’s easy to add “Add” and “Remove” buttons (once you have the artwork) and bind those too?

Why is it hard to make the delete key remove rows?

The technical reason is that you need to subclass NSTableView, override -keyDown: and then somehow interact with the data source to remove the item in question.

Nuts to that. Drop NSTableView+CocoaBindingsDeleteKey.m into your Xcode project and get it for free. Well, extremely cheap.

The technique used here is a sledgehammer — it enables the delete key on all bound NSTableViews, and does so via a category. But it’s an extremely cheap method to get that feature working. If you need subtlety, you’ll need to write your own code.

Update: Thanks to Sascha Freese for pointing out my category suppresses NSTableView’s arrow-key navigation. Ouch. I rewrote it into an NSTableView poser, and it now calls through to -[NSTableView keyDown:] instead of fully replacing it. Using it remains the same, fortunately: just add NSTableView+CocoaBindingsDeleteKey.m to your project.

Friday, May 26, 2006
12:00 AM