Reputation: 545
The setup:
My ViewController code is this:
import Cocoa
class ViewController: NSViewController, NSTableViewDataSource {
@IBOutlet var tableView: NSTableView!
let data = [
["FirstName": "Debasis", "LastName": "Das"],
["FirstName": "Nishant", "LastName": "Singh"],
["FirstName": "John", "LastName": "Doe"],
["FirstName": "Jane", "LastName": "Doe"],
["FirstName": "Mary", "LastName": "Jane"]
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.setDataSource(self)
}
override var representedObject: AnyObject? {
didSet {
}
}
func numberOfRowsInTableView(tview: NSTableView) -> Int {
return data.count
}
func tableView(tview: NSTableView, objectValueForTableColumn col: NSTableColumn?, row: Int) -> AnyObject? {
return "row \(row) col \(col?.identifier)"
}
}
There is a connection from the tableView outlet to the NSTableView in the storyboard.
When this app is run, you get a a table view with 5 rows as you'd expect given the data, but the contents of each cell is "Table View Cell". I've tried just returning "1", the number 1, various other objects, but I can't get it to display what I return from the tableView method. I'm assuming that the AnyObject returned needs to be something specific (as in there's an instanceof check in the caller), but all the sample code I've found returns strings and things work, so either things have changed or my code is wrong in some way I haven't been able to figure out.
Upvotes: 0
Views: 2158
Reputation: 545
I found the answer here, after searching the Apple Dev Forums:
Specifically the bindings approach works easily.
Upvotes: 1
Reputation: 9499
If you are just doing a view that contains a Table view, I recommend just using a UITableViewController
instead. Create one from the template, should be much easier for you to get this up and running.
Upvotes: 0