NSTableView not setting the content right

I'm using the NSTableView that is available in interface builder. I'm using Xcode 8. Then I set the datasource to one of my view controller. I implemented the following:


func numberOfRows(in tableView: NSTableView) -> Int {

return 1

}

public func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?

{

return "A"

}


However when I run this, I just see table view with one row, two column, and both displaying the following text:

"Table View Cell"


It used to be much simpler, now the documentation says something, header has something else.

Replies

Actually, I was able to get the content displayed. Following guide was helpful although different then what is current working:


https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TableView/PopulatingView-TablesProgrammatically/PopulatingView-TablesProgrammatically.html


I then had to match the identifier of the cell with the column.

Well, you haven't really implemented this completely. Assuming you have placed a table with view-based cells rather than NSCell-based cells…


Each column of each row has a cell view, which is a NSTableCellView (or a subclass of NSTableCellView, if you've chosen to go that route). A cell view has two standard subviews, "imageView" and "textField". The unconfigured default behavior of textField is to show that static text "Table View Cell".


What you need to do, to finish your implementation, is to connect textField to the value for each row, and there are a couple of possible ways of doing that:


1. You can bind the "value" binding of the text field to a value provided via the cell view's "objectValue" property. Since your data source is providing the string "A" as the object value for every row, your binding would just use the model key "objectValue". (Typically, the objectValue is a custom class instance with custom properties, you usually use a model key of the form "objectValue.whateverProperty", but a simple String value does work.)


2. You can omit the tableView(_:objectValueFor:row:) method completely, and implement the delegate method tableView(_:viewFor:row:) and set the "objectValue" property of the cell view in that method. This is more or less equivalent to doing #1.


3. You can omit the tableView(_:objectValueFor:row:) method completely, and implement the delegate method tableView(_:viewFor:row:). In that method you can set the value of the text field directly.

I was able to get it working by implementing the following method:


func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?


Then I obtained view as :


tableView.make(withIdentifier: identifier, owner: self) as! NSTableCellView


Then set the textfield's string value.