You have a delegate function for tableView that just do this :
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
It gets data from the dataSource to display in the table
You have to set a unique identifier each column in IB : "name", "age"
You have your data source (an array with 2 columns) that stores your data: items[row] is an array (you could alos use a dictionary)
Here is from an excellent online tutorial (CocoaProgramming from AppleProgramming) an example
import Cocoa
class Person: NSObject {
var name : String
var age : Int
override init() {
self.name = String()
self.name = "Yoda"
self.age = 300
super.init()
}
init(name: String, age: Int) {
self.name = name
self.age = age
}
required convenience init?(coder decoder: NSCoder) {
self.init()
self.name = decoder.decodeObjectForKey("name") as! String
self.age = decoder.decodeObjectForKey("age") as! Int
}
}
/
extension Person: NSCoding {
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
coder.encodeObject(Double(self.age), forKey: "age")
}
}
and the controller
class TableViewController: NSObject, NSTableViewDataSource {
@IBOutlet weak var tableView: NSTableView!
var list : [Person]
override init () {
list = [Person]()
super.init()
}
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return list.count
}
func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
let p = list[row]
let identifier : String = (tableColumn?.identifier)!
let s = p.valueForKey(identifier as String)
return s
}
func tableView(tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row: Int) {
let p = list[row]
let identifier : String = (tableColumn?.identifier)!
p.setValue(object, forKey: identifier as String)
tableView.reloadData()
}
@IBAction func add(sender: NSButton) {
let p = Person(name: "aName", age: 20)
self.list.append(p)
tableView.reloadData()
}
@IBAction func remove(sender: AnyObject) {
let row = tableView.selectedRow
tableView.abortEditing() /
if row >= 0 {
list.removeAtIndex(row)
tableView.reloadData()
}
}
}