NSTableView does not show last column

47 Views Asked by At

I am programmatically creating an NSTableView with a set of 6 columns, the parameters of which are defined in an Enum:

public enum SearchResultFields: Int, Codable, CaseIterable {

case symbol = 0
case name = 1
case securityClass = 2
case countryCode = 3
case currencyCode = 4
case ISIN = 5


public var displayString: String {
    
    switch self {
    case .symbol: return "Symbol"
    case .name : return  "Name"
    case .securityClass : return  "Type"
    case .countryCode : return  "Country"
    case .currencyCode : return  "Currency"
    case .ISIN: return "ISIN"
    }
}


var macOSDefaultWidth: CGFloat {
    
    switch self {
        
    case .symbol: return  100
    case .name: return  200
    case .securityClass: return  80
    case .countryCode: return  100
    case .currencyCode: return  80
    case .ISIN: return  100
    }
    
}

When the window displaying the table appears, I size the window so that its width is exactly the same as the width of all the columns in the table (plus a 16pt spacer on each side of the table):

let defaultWidth = SearchResultFields.allCases.map({ $0.macOSDefaultWidth }).reduce(0, +) + 16 * 2
print("deafultWidth = \(defaultWidth)")
                
let oldFframe = self.searchWindowController?.window?.frame ?? CGRect(x: 200, y: 200, width: 600, height: 400)
let newFrame = self.userSettings.searchWindowFrame ??  CGRect(x: oldFframe.minX, y: oldFframe.minY, width: defaultWidth, height: 600)
self.searchWindowController?.window?.setFrame(newFrame, display: true)
  

The table is created as the .documentView in an NScrollView and it does not allow autoresizing, and each column is then added to the table, iterating the Enum:

self.resultsView.columnAutoresizingStyle = .noColumnAutoresizing
    
for searchField in SearchResultFields.allCases {
                
   let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: searchField.displayString))
                
   let headerCell = CustomTableHeaderCell(parentTable: self.resultsView, standards: standards, textCell: searchField.displayString)
   column.headerCell = headerCell
   column.resizingMask = .userResizingMask
       
   column.width = searchField.macOSDefaultWidth
   column.minWidth = 30
   column.maxWidth = 1000
                
              
   let descriptor = NSSortDescriptor(key: searchField.displayString, ascending: true)
                column.sortDescriptorPrototype = descriptor
   print("Adding Column: \(searchField.displayString) - width: \(column.width)")
                self.resultsView.addTableColumn(column)
}
  

The the program runs and the window is displayed, all the printouts match expectations: the columns are added with the sizes provided by the Enum and the window size is exactly the sum of all the column widths plus the 16pts spacers on each side of the table.

Adding Column: Symbol - width: 100.0
Adding Column: Name - width: 200.0
Adding Column: Type - width: 80.0
Adding Column: Country - width: 100.0
Adding Column: Currency - width: 80.0
Adding Column: ISIN - width: 100.0

deafultWidth = 692.0
width: 692.0
SearchVC Bounds: Optional((0.0, 0.0, 692.0, 572.0))

BUT the window only displays the first 5 columns, not the last one. It is there, but you need to scroll the view to see it, which is not the intended result.

enter image description here

Has anyone have any idea as to what I am doing wrong?

Thanks

1

There are 1 best solutions below

0
Marco On

Thanks to @willeke I have found why this happened: i did not set an intercellSpacing for the NSTableview, but when I printed it out was set at a with of 17 (I don't know where this value came from).

Setting .intercelleSpacing to {0, 0} resolved my problem and now the view appears as it should with all the columns