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?