Hello fellow programmers,
First off, I am new to OSX/iOS programming and am trying to learn swift. I am fairly competent in java and wanted to teach myself something new
Yes, tableviews with Cocoa are... complicated.
The absolute basics for getting content in the tableview is to set its datasource, which has to adopt NSTableViewDataSource
protocol. Then you have to differentiate between a view based and a cell based tableview. In the view based you can use a view you designed in Interface Builder, the cell based are older and simpler. You understand the numberOfRowsInTableView
function, so I proceed with the more complicated functions.
For cell based tableViews, the second essential function## Heading ## is
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!
When you just got it, it's pretty easy. As first parameter you get the tableView (only interesting if you use the same dataSource for multiple tableViews). The second specifies the tableColumn. You can identify a column by using its identifier. You set this identifier in the InterfaceBuilder. Click on your tableview until a column is selected. Then set in the sidebar the Restoration ID. As last parameter you get your row. You return an object of type AnyObject. I normally return a string, I don't know whether NSNumber is also valid. A simple example implementation is the following:
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!{
var result = ""
var columnIdentifier = tableColumn.identifier
if columnIdentifier == "number" {
result = "\(row+1)"
}
if columnIdentifier == "name" {
result = model.persons[row].name
}
if columnIdentifier == "lastName" {
result = model.persons[row].lastName
}
return result
}
When you want to set values, use
func tableView(tableView: NSTableView!, setObjectValue object: AnyObject!, forTableColumn tableColumn: NSTableColumn!, row: Int)
Object represents the new value you should transfer to your data model.
In view based tableViews, the things lays different.
Here you have to design a custom view in a separate nib file. Then, you register the nib in your initializer.
let nib = NSNib(nibNamed: "Example", bundle: NSBundle.mainBundle())
self.tableView.registerNib(nib, forIdentifier: "Example")
This allows you to create instances of your view using makeViewWithIdentifier:owner:
Then you can configure your view (I like to subclasses NSView for this) and pass it back as result of
func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView!
Example implementation:
func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView!{
let view = tableView.makeViewWithIdentifier("Example", owner: self) as MyCustomView
view.field1.stringValue = model.persons[row].name
view.field2.stringValue = model.persons[row].lastName
view.field3.stringValue = "\(row+1)"
return view
}