Hide/Show tableview columns?

I am using Xcode 11.6 and IB for a macOS app and using a tableview. It has 10 columns. I would like to add the functionality to right click on the header and have a pop menu with the column names to select which columns for the tableview to show/hide. What is the best approach to add this functionality? How is this done?


Did you already succeed to hide only the current column ?

You should use
Code Block
func tableView(NSTableView, mouseDownInHeaderOf: NSTableColumn)

Which tells the delegate that the mouse button was clicked in the specified table column’s header.

Note: do you intend to do multiple selection in the popup ? May be not the best UI option. Why not a popover where to select the columns ?
In this func, you can test for right click before calling popup.
To change width of a column:
Code Block
let zeroWidth = 0.0
self.tableView.tableColumns[column].width = zeroWidth

Yes, I was thinking to allow multiple selections in the popup, then setting the columns to zero width which are not selected. Why do you say this is not the best UI option? I am understanding you correctly?

Thanks
Depending how careful you are, it's possible to accomplish the whole thing in a category on NSTableHeaderView (for standard TableViews). It wouldn't allow multiselection, but would immediately apply to all TableViews.
  • Override -[NSTableHeaderView menu] to generate one if not already set, store it, then set the delegate to self

  • Implement the NSMenuDelegate methods to populate the menu when opened (really just before)

  • Implement an action to handle the NSMenuItem click and setHidden: on the NSTableColumn

Yes, I was thinking to allow multiple selections in the popup, then setting the columns to zero width which are not selected. Why do you say this is not the best UI option? I am understanding you correctly?

Usually a popup closes once selection is done. If you want multiple selection, you need a way to end selection, outside of the popup. Not very user friendly.
Hide/Show tableview columns?
 
 
Q