[英]Xcode Cocoa - Drag Drop from NSTableView to Finder
我希望我的MacOS App能夠將項目從NSTableView拖到其他應用程序,如Logic Pro X,Finder等。此TableViews中的項目是我創建的類,它們代表HD上的文件。
public class AudioFile
{
#region Computed Propoperties
public string Filename { get; set; } = "";
public string Filepath { get; set; } = "";
#endregion
public AudioFile()
{
}
public AudioFile(string filename, string filepath)
{
this.Filename = filename;
this.Filepath = filepath;
}
}
不幸的是,我找不到可以轉換為C#(Xamarin)的Swift或Objective-C解決方案。 有誰知道一個或有一些代碼可以幫助您?
謝謝你的幫助!
我對C#一無所知,但您要求使用Swift或Objective-C解決方案。 我可以幫忙的! 下面是Swift 4。
首先,確保您的ViewController
是表視圖的數據源:
class ViewController: NSViewController, NSTableViewDataSource
您還需要使用代碼或IB建立該連接。
然后,您需要將表格視圖設置為拖動源。 選擇所需的操作,通常是.move
或.copy
:
tableView.setDraggingSourceOperationMask(.move, forLocal: false)
本示例假定您使用ArrayController管理tableView的內容。 您確實應該,它使許多事情變得容易。 另外,此示例用於拖動多個文件。 (它僅適用於單個文件,但是如果您只想拖動一個文件,則還有其他方法。)
在您的ViewController類中,實現此方法:
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
var filePaths = [String]()
// Swift 4 hack--the FilenamesPboardType is missing
let NSFilenamesPboardTypeTemp = NSPasteboard.PasteboardType("NSFilenamesPboardType")
pboard.addTypes([NSFilenamesPboardTypeTemp], owner: nil)
if let audioFiles = audioFilesArrayController.arrangedObjects as? [AudioFile] {
for i in rowIndexes {
filePaths.append(audioFiles[i].Filepath)
}
}
pboard.setPropertyList(filePaths, forType: NSFilenamesPboardTypeTemp)
return true
}
您可以在此處了解有關NSFilenamesPboardTypeTemp
hack的更多信息。
就是這樣! 重新編譯,您應該可以將一個或多個文件拖到Finder窗口中來移動它們。 簡單。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.