Anyone familar with NSTableColumn Header sort indicators?

I’m trying to create a tableview in Swift with just ONE sortable column (chosen by the user)

a) if the column isn’t currently sortable, set to be so and remove indicator from all others columns
b) if the column is already sortable, invert the indicator

here is what I have tried… and it does “B” properly, and “A” adds the new column indicatior, but doesn’t remove any existing one… to make matters worse… ALL the indicators flip when any is clicked

internal func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
// does this column already have a sort indicator? (x<>0)
let x = tableColumn.headerCell.accessibilitySortDirection().rawValue
if x == 0 { // no sort currently on this column
   // remove indicator from all columns [THIS DOESN'T SEEM TO WORK]
   for i in(0...tableView.numberOfColumns-1) {
      let newCol = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Col\(i)"))
      newCol.sortDescriptorPrototype = nil
   }
   tableView.sortDescriptors.removeAll()
   // Add sort indicator to specified column [This DOES seem to work]
   tableColumn.sortDescriptorPrototype = colSortDescriptor
   tableView.reloadData()
   }
}

Solved! :slight_smile:

internal func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
   // does this column already have a sort indicator? (x<>0)
   let x = tableColumn.headerCell.accessibilitySortDirection().rawValue
   if x == 0 { // no sort currently on this column
      for i in(0...tableView.numberOfColumns-1) {
         let newCol = theTABLE!.tableColumns[i]
         if newCol == tableColumn {
            let colSortDescriptor  = NSSortDescriptor(key:  "Col\(i)", ascending: true, selector: nil)
            newCol.sortDescriptorPrototype = colSortDescriptor
         } else {
            newCol.sortDescriptorPrototype = nil
         }
      }
      tableView.reloadData()
   }
}