【问题标题】:Get destination path while dragging and dropping from NSTableView从 NSTableView 拖放时获取目标路径
【发布时间】:2018-01-05 19:53:29
【问题描述】:

我有 NSTableView,我希望它支持拖放。我成功地处理了从 finder 中删除 nstableview 的问题。也能够从 nstableview 拖放到 finder,但它会在目的地创建文本剪辑文件。但我只想要目标路径。我还发现了一篇关于 NSFilesPromisPboardType 的帖子,该帖子也可以正常工作,但效果不佳。 (在使用 NSFilesPromisePboardType 方法时,我无法重新排序行)。

我的问题是如何启用行的重新排序以及拖动到查找器(应该只获取目标路径)。

【问题讨论】:

  • 调用draggingSession:sourceOperationMaskForDraggingContext: 两次,两个值都是NSDraggingContext
  • 但它会返回我丢弃它的路径吗?我只想要一个放置位置的路径,以便在放置完成时进一步执行,而不是复制、移动等。对不起,菜鸟问题。

标签: objective-c cocoa


【解决方案1】:

我终于解决了。

#define MyPrivateTableViewDataType @"MyPrivateTableViewDataType"


[self.tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
[self.tableView registerForDraggedTypes:[NSArray arrayWithObjects:MyPrivateTableViewDataType,NSFilesPromisePboardType,NSFilenamesPboardType,nil]];



//handling drag and drop

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

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObjects:MyPrivateTableViewDataType,NSFilenamesPboardType,nil] owner:self];


NSPoint dragPosition;
NSRect imageLocation;

NSEvent *event =[NSApp currentEvent];
dragPosition = [tv convertPoint:[event locationInWindow] fromView:nil];

dragPosition.x -=16;
dragPosition.y -=16;

imageLocation.origin=dragPosition;
imageLocation.size=NSMakeSize(32, 32);
[tv dragPromisedFilesOfTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil] fromRect:imageLocation source:self slideBack:YES event:event];

return YES;
}


-(NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination{
NSString *str=[dropDestination path];
NSLog(@"%@", str);
NSMutableArray *rootDraggedItems=nil;
return  rootDraggedItems;

}

-(NSDragOperation)tableView:(NSTableView *)tv validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation{
if ([info draggingSource] !=tv) {
    return  NSDragOperationCopy;
}
return NSDragOperationNone;
}

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

NSPasteboard *pboard = [info draggingPasteboard];

NSArray *files=[pboard propertyListForType:NSFilenamesPboardType];
NSInteger i;
for (i=0; i<[files count]; i++) {
    NSString *filePath = [files objectAtIndex:i];
    NSString *path =[filePath stringByStandardizingPath];
    NSLog(@"%@", path);
}


NSLog(@"%ld", (long)row);


return YES;
}

我知道这并不完美,但至少目前有效。

【讨论】:

  • 现在的问题是,如何更改拖动图标?
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
相关资源
最近更新 更多