Swift displayed in OS X Tableview
Preparation
First of all, your ViewController must inherit from
NSTableViewDelegate and NSTableViewDataSource:class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource
Then, like in iOS you must implement the delegate methods for the
NSTableView.
Make an
IBOutlet, something like:@IBOutlet weak var tableView: NSTableView!
Delegate and DataSource
Then in the viewDidLoad(), set the delegate and the dataSource to self:
tableView.setDelegate(self)
tableView.setDataSource(self)
And implement the required methods:
For the number of rows:
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return yourData.count //your data ist the array of data for each row
}
For the row data:
func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
if tableColumn!.title == "firstColumnTitle" //if you have more columns
{
return yourData[row].first
}
else //second column
{
return return yourData[row].second
}
}
For selection: (optional)
func tableViewSelectionDidChange(notification: NSNotification) {
var row = tableView.selectedRow
// a row was selected, to something with that information!
}