【问题标题】:Getting array objects into NSTableView将数组对象放入 NSTableView
【发布时间】:2016-11-17 10:51:09
【问题描述】:

我想从一个数组中捕获信息并将其显示到我的 NSTableView 中。我不确定我需要为此做什么(我对 Swift 和一般编程非常陌生)。

我的表格视图如下所示:

我想从我的数组中获取值名称,并使用 NSTableView 将其显示在名称表中。我找到了this tutorial on Ray Wenderlich,但代码已经过时了,我不想在我的项目中使用旧的东西,这些东西在较新的操作系统版本中可能不再适用。

看来我需要[NSTableViewDataSource numberOfRows][3]viewFor

关于如何做到这一点的任何例子 - 也许有人在几周前用 Swift 3 做了这个? :D

数组中的信息将由以下生成:

var devices = [Device]()
    let quantityDevices = quantityData.intValue

    for i in 0...quantityDevices-1 {

        let newDevice = Device()
        print("created new device")

        newDevice.name = titleData.stringValue + "-\(i)"
        devices.append(newDevice)
    }

    print("now we have \(devices.count) devices in our array")


}

【问题讨论】:

    标签: arrays swift macos


    【解决方案1】:

    您需要的代码的重要部分是 DataSource 委托函数:

    extension ViewController : NSTableViewDataSource {
      func numberOfRows(in tableView: NSTableView) -> Int {
        return devices.count
      }
    
      func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    
        // 1 - get the device for this row
        guard let device = devices[row] else {
          return nil
        }
    
        // 2 - configure the cell with the device data
        return nil
      }
    

    有一个例子here on StackOverflow应该给出一个更好的例子

    【讨论】:

    • 感谢您的帖子!我正在使用 OS X 和 NSTableView,而不是在 iOS / UITableView 上 - 就是这样。有这方面的信息吗?
    • 对不起,我没有注意到标签!。呃,我看一下,我觉得挺像的
    • 没问题,一切就绪。我有时间! :D
    • raywenderlich.com/118835/os-x-nstableview-tutorial 这应该会给你所有你需要的信息......可能更多。我会尽快更新我的答案
    • 这就是我上面提到的我之前发现的,对我没有太大帮助,因为它是 Swift 2.2 代码。要了解它的作用,示例代码对我来说是最好的,因为我可以设置断点并理解它在做什么。完成后告诉我:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 2015-01-10
    • 2017-07-20
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多