在swift中将数据加载到NSTableView

时间:2014-12-10 09:48:49

标签: objective-c macos swift nstableview appdelegate

我是IOS / IOX开发的新手。我尝试了很多站点使用NSTableView在OSX swift上将数据加载到我的表中。但都是失败。 我所做的程序是引用View-Based NSTableView in Swift - How to

  1. 拖动并下载NSTableView,这在我运行代码时非常明显。
  2. 选择第一列,并将标识符设为"列表"将列数设置为1
  3. 之后
  4. 在我粘贴的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
    
        }
    }
    
  5. 然后我尝试通过选择表并指向" App Delegate"来设置委托和数据源作为表视图的Appdelegate。在"应用场景"。但它没有链接

  6. 当我运行程序时,加载了没有单元格的表
  7. 非常感谢任何帮助。

3 个答案:

答案 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

因此,只需将代码从应用程序委托移动到视图控制器并连接故事板中的所有内容。