0

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

Marco
  • 81
  • 1
  • 8
  • Not sure I am following you. The columns add up to 660 (100 + 200 + 80 + 100 + 80 + 100). Adding the 32 of spacers give 692. Also, the defaultWidth in the printout is actually calculated by the program. The only manual inputs for sizes are in the Enum. All the rest is calculated – Marco May 19 '23 at 15:29
  • 1
    I have modified the question to make it clear that the spacers are at the side of the table – Marco May 19 '23 at 16:02
  • Is the window to small or are the columns too big? – Willeke May 19 '23 at 16:50
  • 1
    What is the value of `resultsView.intercellSpacing`? – Willeke May 19 '23 at 17:26
  • The window size and the column sizes are all correct(!): – Marco May 19 '23 at 17:30
  • This is the printout I get: All sizes are: [100.0, 200.0, 80.0, 100.0, 80.0, 100.0] Intercell Spacing = (17.0, 0.0) SearchVC Frame: Optional((521.0, 375.0, 692.0, 600.0)) – Marco May 19 '23 at 17:30

1 Answers1

0

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

Marco
  • 81
  • 1
  • 8