LockFocus

A (Slightly) Improved NSTableView

While working on Lexicon and Opacity, I’ve used NSTableViews a lot. They work very well, but there are a few convenient behaviors that they don’t have out of the box.

Some of the features that were missing in Tiger were type to select, source list appearance, and tooltips to show the text of a cell when it’s clipped. I implemented these and other behaviors for Lexicon, only to have them built in to NSTableView in Leopard! (not that I’m bitter) Check the AppKit release notes for all of the details on what’s new in NSTableView.

For Opacity, I put together a subclass of NSTableView that adds a few more features-specifically for handling keyboard presses. LTKeyPressTableView adds enter or return to edit a row, delete to delete the current row, and hooks to intercept left and right key presses.

View LTKeyPressTableView code

LTKeyPressTableView uses the table view’s existing delegate, adding a few optional delegate methods. Implement deleteSelectionFromTableView: to do something when the user presses delete with a row (or rows) selected, and goLeftFromTableView: or goRightFromTableView: for when they press the corresponding arrow keys. Enter automatically starts editing the first editable column of the table (if there is one) unless you implement preventEnterEditingTableView: to return YES. (Note: LTKeyPressTableView requires Leopard.)

By using LTKeyPressTableView for your tables, you can easily add user-friendly behavior with just a few delegate methods. As an added bonus, a key-press version of NSOutlineView is also included. Try them out!