获取数组对象到NSTableView中
问题描述:
我想从数组中捕获信息并将其显示到我的NSTableView中。我不确定我需要做什么(我对Swift和编程一般都很陌生)。获取数组对象到NSTableView中
我的表视图看起来是这样的:
我想从我的数组中获取价值的姓名和名称表中与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")
}
答
你需要的代码的重要的部分是数据源委托功能:
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 *应该举一个更好的例子
谢谢你的发表!我正在使用OS X和NSTableView,而不是iOS/UITableView - 就是这个问题。 这方面的任何信息? – Jakob
对不起,我没有注意到标签! Err,我会快速浏览一下,我认为它非常相似 – Scriptable
没问题,所有设置。我有时间! :D – Jakob