【问题标题】:NSOpenPanel to choose files and populate a NSTableViewNSOpenPanel 选择文件并填充 NSTableView
【发布时间】:2015-10-08 08:08:47
【问题描述】:

我想知道如何通过首先通过 NSOpenPanel 获取文件来填充 NSTableView。这就像我在运行时从文件夹中选择后列出/显示文件一样。

编辑:所以我有这种工作(通过绑定)....但是除非我单击表格顶部,否则我无法显示它。如果您打开以获取更多文件,则显示将不会再次更新。

我觉得这可能是因为 controllerArray 的 Received Action add: 没有链接。 下面的代码就是我现在所拥有的

@IBAction func openPanel(sender: NSButton) {
    let openPanel = NSOpenPanel()
    openPanel.allowedFileTypes = ["pdf"]
    openPanel.canChooseFiles = true
    openPanel.allowsMultipleSelection = false
    openPanel.canChooseDirectories = true
    openPanel.beginWithCompletionHandler { (result) -> Void in

        if result == NSFileHandlingPanelOKButton {
            var url:NSURL = openPanel.URL!
            var pdfFile: PDFModel = PDFModel()
            pdfFile.initWithURL(url)
            self.insertFileLibrary(pdfFile, inFileListAtIndex: self.fileLibrary.count)

        }
    }
}


func insertFileLibrary(pdfModel:PDFModel, inFileListAtIndex index:Int){
    fileLibrary.insertObject(pdfModel, atIndex: index)       
}

func removeObjectFromFileLibraryAtIndex(index:Int){
        fileLibrary.removeObjectAtIndex(index)
    }

【问题讨论】:

  • 如果你在 addToTable 中设置断点,fileList、table 和 doc 是你所期望的吗?
  • 对不起,我没有关注....

标签: macos swift cocoa nstableview nsopenpanel


【解决方案1】:

绑定使用 KVO,您必须以 KVO 的方式改变 fileLibrary,并被 willChange... 和 didChange... 包围。让 NSArrayContoller 更容易做到这一点。您可以使用 NSArrayController 的 addObject(s)、insertObject(s) 和 removeObject(s) 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多