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.
// make return and tab only end editing, and not cause other cells to edit
- (void) textDidEndEditing: (NSNotification *) notification
{
NSDictionary *userInfo = [notification userInfo];
int textMovement = [[userInfo valueForKey:@"NSTextMovement"] intValue];
if (textMovement == NSReturnTextMovement
|| textMovement == NSTabTextMovement
|| textMovement == NSBacktabTextMovement) {
NSMutableDictionary *newInfo;
newInfo = [NSMutableDictionary dictionaryWithDictionary: userInfo];
[newInfo setObject: [NSNumber numberWithInt: NSIllegalTextMovement]
forKey: @"NSTextMovement"];
notification =
[NSNotification notificationWithName: [notification name]
object: [notification object]
userInfo: newInfo];
}
[super textDidEndEditing: notification];
[[self window] makeFirstResponder:self];
} // textDidEndEditing
(Thanks to Steven Jacowski for a tweak that ends editing on clicks on different cells)NSTableViewDropAbove- (NSDragOperation) tableView: (NSTableView*) tableView
validateDrop: (id ) info
proposedRow: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
int result = NSDragOperationNone;
if (op == NSTableViewDropAbove) {
result = NSDragOperationMove;
}
return (result);
} // validateDrop
NSTableView's -setDoubleAction: method, and supply it a standard IBAction-style method selector.
You may need to also do -setTarget:. double-clicks get sent if the column isn't editable, so you may need to grab the column and do a -setEditable: NO on it.
- (BOOL) tableView: (NSTableView *) tableView
writeRowsWithIndexes: (NSIndexSet *) rowIndexes
toPasteboard: (NSPasteboard *) pboard;
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)
- (void) textDidEndEditing: (NSNotification *) notification
{
NSDictionary *userInfo;
userInfo = [notification userInfo];
NSNumber *textMovement;
textMovement = [userInfo objectForKey: @"NSTextMovement"];
int movementCode;
movementCode = [textMovement intValue];
// see if this a 'pressed-return' instance
if (movementCode == NSReturnTextMovement) {
// hijack the notification and pass a different textMovement
// value
textMovement = [NSNumber numberWithInt: NSIllegalTextMovement];
NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject: textMovement
forKey: @"NSTextMovement"];
notification = [NSNotification notificationWithName:
[notification name]
object: [notification object]
userInfo: newUserInfo];
}
[super textDidEndEditing: notification];
} // textDidEndEditing
mouseDownInHeaderOfTableColumn or didClickTableColumn:
NSImage *indicatorImage;
if (sortAscending) {
sort your data ascending
indicatorImage = [NSImage imageNamed: @"NSAscendingSortIndicator"];
} else {
sort your data descending
indicatorImage = [NSImage imageNamed: @"NSDescendingSortIndicator"];
}
sortAscending = !sortAscending;
[tableView setIndicatorImage: indicatorImage
inTableColumn: tableColumn];
[tableView reloadData];
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
int row;
row = [tableView selectedRow];
if (row == -1) {
do stuff for the no-rows-selected case
} else {
do stuff for the selected row
}
} // tableViewSelectionDidChange
If you have more than one tableview, the notification's object is the tableview that had the selection change.-setDropRow:dropOperation:, like so:
- (NSDragOperation) tableView: (NSTableView *) view
validateDrop: (id ) info
proposedRow: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
// have the table highlight on-row / between-row correctly
[view setDropRow: row
dropOperation: op];
// or use whatever drag operation is appropriate
NSDragOperation dragOp = NSDragOperationCopy;
return (dragOp);
} // validateDrop
and in the acceptDrop method, look at the operation:
- (BOOL) tableView: (NSTableView *) view
acceptDrop: (id ) info
row: (int) row
dropOperation: (NSTableViewDropOperation) op
{
if (op == NSTableViewDropOn) {
// replace existing
} else if (op == NSTableViewDropAbove) {
// add new
} else {
NSLog (@"unexpected operation (%d) in %s",
op, __FUNCTION__);
}
// documentation doesn't actually say what this signifies
return (YES);
} // acceptDrop
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
- (id) tableView: (NSTableView *) tableView
objectValueForTableColumn: (NSTableColumn *) tableColumn
row: (NSInteger) row