El Tomato
El Tomato

Reputation: 6705

How Do you Drag-Move a Row in NSTableView?

If it were an iOS project, letting the user drag-move the selected row would be relatively easy. I think it's something like

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    // ...
}

How do you do it with NSTableView (not UITableView in iOS) in Mac OS if I have a cell-based table? One sample project by Apple, Inc. that lets the user drag items is image-browser, which doesn't quite help because it doesn't involve an NSTableView control. Do you use canDragRowsWithIndexes:atPoint? I don't get many search hits with canDragRowsWithIndexes. If I run a search for "NSTableView reorder," I get hits for sorting table items.

Thank you for your advice.

Upvotes: 4

Views: 4646

Answers (1)

Wain
Wain

Reputation: 119041

Use these methods:

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id < NSDraggingInfo >)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation

- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation

- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard

This guide by Corbin Dunn is a little old but should get you there well.

Upvotes: 3

Related Questions