【问题标题】:Unable to populate xib-created swift tableView using macOS无法使用 macOS 填充 xib-created swift tableView
【发布时间】:2020-02-08 06:29:09
【问题描述】:

我无法使用 Xcode 11.2 xib 在 macOS 10.14.6 中填充基于单元格的快速表格视图。该应用程序是基于文档的,并且 tableView 是使用单独的 WindowController xib 创建的。在 Xcode 中以编程方式创建的类似项目可以正常工作,包括拖放;我对使用 xibs 比较陌生,可能没有正确设置。在 xib 中设置了列标识符,并且 NSTableViewDataSource 和 NSTableViewDelegate 已添加到窗口控制器。相关源代码如下,完整的Xcode项目可以在这里下载:https://www.dropbox.com/s/6tsb98b7iihhfxl/tableView.zip?dl=0

在使用 String 数组填充 tableView 的任何帮助将不胜感激。我也想让拖放工作,但现在只需让数组项显示在表格视图中就可以了。它正确地创建了四行,对应于数组中的元素数量,但没有可见的文本。 tableView 是基于单元格的,但如果效果更好,我可以使用基于视图的。提前谢谢你。

class WindowController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate {

@IBOutlet var tableView: NSTableView!

var sports : [String] = ["Basketball","Baseball","Football","Tennis"]

    override func windowDidLoad() {
        super.windowDidLoad()
        tableView.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
        tableView.dataSource = self
        tableView.delegate = self
    }

    func numberOfRows(in tableView: NSTableView) -> Int {
        return (sports.count)
    }

    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        var value : Any? = 0
        let columnIdentifier : String = (tableColumn?.identifier.rawValue)!
         if (columnIdentifier == "Col1"){
            value = sports[row]
        }
        return value
     }

【问题讨论】:

标签: swift macos


【解决方案1】:

在 Document.swift 中,windowControllershowTableView() 的末尾释放,并且 table view 失去了它的数据源。将windowController 添加到文档的窗口控制器或保持对windowController 的强引用。

@IBAction func showTableView(_ sender: Any) {
    let windowController = WindowController.init(windowNibName:NSNib.Name("WindowController"))
    addWindowController(windowController)
    windowController.showWindow(nil)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多