How Do you Drag-Move a Row in NSTableView?

∥☆過路亽.° 提交于 2019-12-06 08:32:39

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16939623/how-do-you-drag-move-a-row-in-nstableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!