gpt4 book ai didi

macos - 拖放到 NSArrayController

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:32 25 4
gpt4 key购买 nike

我正在努力将通过拖放(文件从 finder 到 NSView)接收到的变量传递给 AppDelegate 中的 NSArrayController。

NSView 接收拖放:

class MyView:NSView, NSDraggingDestination {

override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}

let fileTypes = ["jpg", "jpeg", "bmp", "tif", "TIF"]
var fileTypeIsOk = false
var droppedFilePath = ""


required init?(coder: NSCoder) {
let types = [NSFilenamesPboardType, NSURLPboardType, NSPasteboardTypeTIFF]
super.init(coder: coder)
registerForDraggedTypes(types)
}
override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {

if checkExtension(sender) == true {
fileTypeIsOk = true
return .Every
} else {
fileTypeIsOk = false
println("Dropped images rejected.")
return .None
}
}
override func performDragOperation(sender: NSDraggingInfo) -> Bool {

var go = false
if let board = sender.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {
for indexFile in 0...board.count-1 {
println("Files dropped \(board[indexFile])")
//-> pass the variable board[indexFile] or board[] to NSArrayController

go = true
}
}
return go
}

func checkExtension(drag: NSDraggingInfo) -> Bool {
var go = false
var foundExtension = 0
var numberOfFiles = 0

if let board = drag.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {
numberOfFiles = board.count
for indexFile in 0...numberOfFiles-1 {
if let url = NSURL(fileURLWithPath: (board[indexFile] as! String)) {
let suffix = url.pathExtension!
for ext in fileTypes {
if ext.lowercaseString == suffix {
++foundExtension
break
}
}
}
}
}
if foundExtension == numberOfFiles {
println("Dropped files have valid extension.")
go = true
} else {
println("At least one file dropped has an invalid extension.")
}
return go
}
}

在 AppDelegate 中,我设置了一个 NSArrayController(此时指向数据[],但我不知道如何从板[]填充数据[])

func numberOfRowsInTableView(aTableView: NSTableView) -> Int {
return data.count
}

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

var cellView: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView

println("Reload Array")

if tableColumn!.identifier == "fileNameColumn" {
cellView.imageView!.image = NSImage(named: "GreenCircle")
cellView.textField!.stringValue = data[row]

return cellView
}

...

谢谢你的帮助

最佳答案

调用“- (void)addObjects:(NSArray *)objects;” NSArrayController 上的方法并将其传递给董事会:

[self.myArrayController addObjects:board];

关于macos - 拖放到 NSArrayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com