Somebody
Somebody

Reputation: 27

Cell-based NSTableView with custom Cell binding

A cell-based NSTableView has Name column with a NameCell derived from NSTextFieldCell in which, besides the text it draws a custom pie progress indicator (in the very same cell, not in a separate column).

It mimics the one in Finder's Name column, when a big file is downloaded.

I've bound the column's value to the filesAC array controller in order to show the text field,

 columnName.bind(.value, to: filesAC as Any, withKeyPath: "arrangedObjects.fileName", options: nil)

But how to bind the pieProgress of the NameCell to the progress (of the File object in the array) ?

Upvotes: 0

Views: 353

Answers (2)

Somebody
Somebody

Reputation: 27

A cell-based solution would imply overriding dataCell(forRow:) from NSTableColumn in order to return the custom subclassed cell NameCell and set the binding for the NameCell.progress, as per row. But dataCell(forRow:) is deprecated.

A view-based solution is, inside FilesTable class:

self.bind(.content, to: filesAC as Any, withKeyPath: "arrangedObjects", options: nil)
...
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
 ...
  cell = NameCellView(frame:frame)
  cell.bind(NSBindingName(rawValue: #keyPath(NameCellView.progress)), to: cell, withKeyPath: "objectValue.progress", options: nil)
 ...
 }

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90661

I think you would need to bind the column to the objects in the array controller, not the fileName property. That is, use "arrangedObjects" as the key path, not "arrangedObjects.fileName".

Then, your custom cell class will override the setter for objectValue. It would pass the fileName property of the new object on to the superclass but would also retain the object as its own value. You would have to override the getter, too, to return that object.

When you need the value for the progress indicator, you would consult the progress property of the current value object.

All of that said, use a view-based table view. :)

Upvotes: 0

Related Questions