我是IOS / IOX开发的新手。我尝试了很多站点使用NSTableView在OSX swift上将数据加载到我的表中。但都是失败。 我所做的程序是引用View-Based NSTableView in Swift - How to
在我粘贴的appdelegate.swift中
class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification?) {
}
func applicationWillTerminate(aNotification: NSNotification?) {
}
func numberOfRowsInTableView(aTableView: NSTableView!) -> Int {
println("reached 1")
return 10
}
func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView {
println("reached 2")
var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
cell.textField.stringValue = "Hey, this is a cell"
return cell
}
}
然后我尝试通过选择表并指向" App Delegate"来设置委托和数据源作为表视图的Appdelegate。在"应用场景"。但它没有链接
非常感谢任何帮助。
答案 0 :(得分:0)
最简单的方法是在XIB文件中选择表视图,然后在右侧的Utilities面板中选择右边的第三个图标(向右箭头)。还要确保通过单击Interface Builder窗口左下角的图标来显示文档大纲。在Utilities面板中,您只需从委托和数据源圈中拖动到文档大纲中的AppDelegate。
答案 1 :(得分:0)
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
if tableView.identifier=="tableViewIdentifier" {
let cellView = tableView.makeViewWithIdentifier("List", owner: self) as! NSTableCellView
cellView.textField?.stringValue = "Hey, this is a cell"
return cellView
}
return nil
}
将标识符设置为已删除表的tableview也将标识符设置为表列,将委托和数据源设置为包含表的视图控制器。
答案 2 :(得分:0)
在您的情况下,在使用故事板时,您应该将视图控制器作为数据源并委托您的表视图,因为它们包含在一个场景中。该类本身在Xcode项目模板中称为ViewController
。
因此,只需将代码从应用程序委托移动到视图控制器并连接故事板中的所有内容。