Swift NSTableview 拖动选择多行,而不是拖出表



我正在尝试将文件从NSTableView拖放到Finder上。 当我尝试拖动一行时,除了向下拖动表格时最终选择多行之外,没有任何反应。 我的表初始化代码是:

filesList.delegate = self
filesList.dataSource = self
filesList.doubleAction = #selector(doubleClick)
filesList.registerForDraggedTypes([.fileURL])
let descriptorName = NSSortDescriptor(key: "name", ascending: true)
let descriptorSize = NSSortDescriptor(key: "size", ascending: true)
let descriptorAttr = NSSortDescriptor(key: "attr", ascending: true)
filesList.tableColumns[0].sortDescriptorPrototype = descriptorName
filesList.tableColumns[1].sortDescriptorPrototype = descriptorSize
filesList.tableColumns[2].sortDescriptorPrototype = descriptorAttr
filesList.setDraggingSourceOperationMask([.copy], forLocal: false)

后来,我有这段代码,它现在没有多大作用。我希望这能给我放置的文件夹的路径。

func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
guard let board = pboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "NSFilesPromisePboardType ")) as? NSArray,
let path = board[0] as? String
else { return false }
print(path)
return true
}

更新感谢下面Willeke的提示,我能够让它几乎工作。 以下是我更新的代码:

func tableView(_ tableView: NSTableView,pasteboardWriterForRow row: Int)-> NSPasteboardWriting?
{
var filePaths = [String]()
let item = NSPasteboardItem()
let folderPath = URL(fileURLWithPath: NSTemporaryDirectory())
let toPath = folderPath.absoluteString.replacingOccurrences(of: "file://", with: "")
for (_, index) in filesList.selectedRowIndexes.enumerated() {
var fileName = files[index].name
if self.dirPath != "" {
fileName = "(self.dirPath)/(fileName)"
}
let command=""(getImgTool())" get (self.format) "(self.path)" "(fileName)" "(toPath)(fileName)""
//                    let command = "cd ~; (self.getImgTool()) put (self.format) (self.path) (fileName) (filePath)"
let _ = shell(command)
filePaths.append("(toPath)(fileName)")
}
item.setPropertyList(filePaths, forType: .fileURL)
return item
}

我现在遇到的问题是 Finder 不会接受掉落。 Xcode 报告"flavor public.file-url 需要立即获得沙盒扩展数据,但未能获取。 因此,我将 .fileURL 更改为 .URL 和 Finder 接受它,但文件不会写入/复制。 我错过了什么?

您缺少的是,您还需要设置拖动蒙版,以便它允许拖动到其他应用程序,如下所示:

tableView.setDraggingSourceOperationMask (.every, false)

或者类似的东西。

最新更新