I would like to add a method with a default to the NSTableViewDataSource protocol. But when I do this, the default is always called, even when the method is defined in the actual data source. Specifically:
I have a single-column NSTableView which can adopt several different data sources at different times. In one case, I would like the data source to be able to provide not only the values displayed, but the background colors of the rows in the table. In the other cases, the table can be a single color. My idea was to first extend the NSTableViewDataSource protocol:
extension NSTableViewDataSource
{
func tableView (tableView: NSTableView, colorIndexForRow row: Int) -> Int
{
return 0
}
}
public func tableView (tableView: NSTableView, didAddRowView rowView: NSTableRowView, forRow row: Int)
{
let colorIndex = tableView.dataSource()!.tableView(tableView, colorIndexForRow: row)
rowView.backgroundColor = rowColors[colorIndex]
}
public func numberOfRowsInTableView (tableView: NSTableView) -> Int
{
return 100
}
public func tableView (tableView: NSTableView, objectValueForTableColumn column: NSTableColumn?, row: Int) -> AnyObject?
{
return String(format: "This is row %3i", row)
}
public func tableView (tableView: NSTableView, colorIndexForRow row: Int) -> Int
{
return row % 6
}
The reason is because protocol extension doesn't support class polymorphism.
You can rewrite your function as:
public func tableView (tableView: NSTableView, didAddRowView rowView: NSTableRowView, forRow row: Int)
{
let colorIndex = self.tableView(tableView, colorIndexForRow: row)
rowView.backgroundColor = rowColors[colorIndex]
}