Quickies for NSTableView

General

Disabling "Return moves editing to next cell" in TableView[permalink]When you edit cells in a tableview, pressing return, tab, or shift-tab will end the current editing (which is good), and starts editing the next cell. But of times you don't want that to happen - the user wants to edit an attribute of a given row, but it doesn't ever want to do batch changes to everything.

To make editing end, you need to subclass NSTableView and add code to catch the textDidEndEditing delegate notification, massage the text movement value to be something other than the return and tab text movement, and then let NSTableView handle things.

(Thanks to Steven Jacowski for a tweak that ends editing on clicks on different cells)

Dragging feedback only between rows[permalink]When rearranging rows in a tableview, the user feedback should just go between rows (the whole-row highlighting is confusing). In the drop validation method only let things through for NSTableViewDropAbove

in your table view data source (and don't forget to hook up the datasource if you use bindings to populate the tableview)

(Thanks to Rob Rix for updating this with more modern API)

Preventing the return key from editing the next cell[permalink]The default tableview behavior, when editing a row, is to move the field editor to the next cell when the user commits editing using the Return key. If you want Return to commit the editing and dismiss the field editor, subclass NSTableView and add this:

If you have more than one tableview, the notification's object is the tableview that had the selection change.

Table View drag destination on or between rows[permalink]When your NSTableView is a drag destination, you may want to support both dragging onto existing objects (to replace them, or augment them with some new attribute the user is dragging over) and support dragging between existing objects (to insert a new one in between. To get this behavior, you need to use NSTableView's -setDropRow:dropOperation:, like so: