gpt4 book ai didi

swift - 无法使用 macOS 填充 xib 创建的 swift tableView

转载 作者:行者123 更新时间:2023-11-30 10:24:58 25 4
gpt4 key购买 nike

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

任何有关使用字符串数组填充 tableView 的帮助将不胜感激。我还想让拖放工作正常,但现在只需让数组项显示在 TableView 中即可。它正确创建了四行,与数组中的元素数量相对应,但没有可见的文本。 tableView 是基于单元格的,但如果效果更好的话我可以使用基于 View 。预先感谢您。

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
}

最佳答案

在 Document.swift 中,windowControllershowTableView() 末尾释放,并且 TableView 丢失其数据源。将 windowController 添加到文档的窗口 Controller 或保留对 windowController 的强引用。

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

关于swift - 无法使用 macOS 填充 xib 创建的 swift tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60124486/

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