2

I'm trying to create a desktop application for macOS using Swift 3.

My window has two view-based tables, where one table shows a list of packets with the size of the payload. When a packet is selected, the second table shows the payload bytes. Since the payload size varies the second table has programmatically created columns; the largest packet size selected determine the maximum number of columns.

I have no problems if I use cell-based tables, but I prefer to use view-based for both tables.

The columns are created

for i in numCols ..< maxCols {

  let col = NSTableColumn()
  col.identifier = "Byte\(i)ColumnID"
  col.headerCell.stringValue = "[\(i)]"
  tableViewPayload.addTableColumn(col)

}

The problem appears in the viewFor delegate method, where

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

returns nil

It works if I return a NSTextField directly or create a NSTableCellView with a NSTextField as a subview:

let frameRect = NSRect(x: 0, y: 0, width: (tableColumn?.width)!, height: 20)      
let cell = NSTableCellView(frame: frameRect)
cell.identifier = (tableColumn?.identifier)!      

let textField = NSTextField(frame: cell.frame)
textField.stringValue = "Test"
cell.addSubview(textField)

return cell

This approach requires more styling of the cell and I should probably subclass NSTableCellView.

My question is why doesn't the make(withIdentifier:owner:) method work with dynamically created columns? Am I missing something when I create the columns?

Is there a way I can create a NSTableCellView and make use of the textField property?

feks88
  • 21
  • 1
  • `make(withIdentifier:owner:)` loads the cell view from a NIB. It is explained in the documentation. – Willeke Mar 02 '17 at 15:02
  • Thank you for showing that the NSTableCellView requires the NSTextField be added as a subview. Naturally an NSImageView requires the same. Now what I was trying to do works too. – WeakPointer Nov 20 '17 at 21:19

1 Answers1

0

I build entire NSTableViews up programmatically. Assume you are asking about the NSTableView.makeView method. I'm using swift 4.

You know any column you create for the table gets bound to it, and vis versa?

For each column created for the table, I call

column.tableView = tableview
tableview.addTableColumn(column)
WeakPointer
  • 3,087
  • 27
  • 22