Run a function when NSTableView header cell is selected

I am learning NSTableView. I would like to be able to run a function when a header cell is selected and of course, be able to determine which header cell was selected so that column specific code can be run. I have looked at examples on line but am unable to determine how to do it. Any assistance would be appreciated. Below is the struct that represents my custom NSTableView along with the data it populates the table with, which all works.

struct ClosingValue: Identifiable {
    var id: UUID
    var name: String
    var date: String
    var close: Float
    
    static var closingValues = [
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 01, 2009", close: 1.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 02, 2009", close: 2.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 03, 2009", close: 3.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 04, 2009", close: 4.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 05, 2009", close: 5.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 06, 2009", close: 6.10)
    ]
}

struct BetterTableView: NSViewRepresentable {
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
        
        @State var closingValues: [ClosingValue] = ClosingValue.closingValues
        
        func numberOfRows(in tableView: NSTableView) -> Int {
            closingValues.count
        }
        
        func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
            
            var nsView = NSView()
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .center
            
            let attributes1: [NSAttributedString.Key: Any] = [
                .foregroundColor: NSColor.blue,
                .paragraphStyle: paragraphStyle,
                .font: NSFont(name: "Arial", size: 14.0) as Any
            ]
            
            var attributedString = NSAttributedString()
            let tempNSView = NSTextField()
            
            switch tableColumn {
                
            case tableView.tableColumns[0]:
                attributedString = NSAttributedString(string: closingValues[row].name, attributes: attributes1)
                
            case tableView.tableColumns[1]:
                attributedString = NSAttributedString(string: closingValues[row].date, attributes: attributes1)
                
            case tableView.tableColumns[2]:
                let closeAsString = String(format: "$%.2f", closingValues[row].close)
                attributedString = NSAttributedString(string: closeAsString, attributes: attributes1)
                
            default:
                print("problem in table view switch statement")
            }
            
            tempNSView.backgroundColor = NSColor.white
            tempNSView.isBordered = true
            tempNSView.isEditable = false
            tempNSView.attributedStringValue = attributedString
            nsView = tempNSView
            
            return nsView
        }
    } // end of coordinator class

    func makeNSView(context: Context) -> NSScrollView {
        
        let tableView = NSTableView()
        
        tableView.delegate = context.coordinator
        tableView.dataSource = context.coordinator
        tableView.addTableColumn(NSTableColumn())
        tableView.addTableColumn(NSTableColumn())
        tableView.addTableColumn(NSTableColumn())
        tableView.intercellSpacing = NSSize(width: 0.0, height: 0.0)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: NSColor.black,
            .paragraphStyle: paragraphStyle,
            .font: NSFont.systemFont(ofSize: 16)
        ]
        
        let column0AttributedString = NSAttributedString(string: "Stock", attributes: attributes)
        let column0Header = tableView.tableColumns[0]
        column0Header.headerCell.drawsBackground = true
        column0Header.headerCell.backgroundColor = NSColor.systemMint
        column0Header.headerCell.alignment = .center
        column0Header.headerCell.attributedStringValue = column0AttributedString
        column0Header.sizeToFit()
        column0Header.minWidth = 90.0
        column0Header.maxWidth = 90.0
        
        let column1AttributedString = NSAttributedString(string: "Date", attributes: attributes)
        let column1Header = tableView.tableColumns[1]
        column1Header.headerCell.drawsBackground = true
        column1Header.headerCell.backgroundColor = NSColor.systemMint
        column1Header.headerCell.alignment = .center
        column1Header.headerCell.attributedStringValue = column1AttributedString
        column1Header.sizeToFit()
        column1Header.minWidth = 125.0
        column1Header.maxWidth = 125.0
        
        let column2AttributedString = NSAttributedString(string: "Closing", attributes: attributes)
        let column2Header = tableView.tableColumns[2]
        column2Header.headerCell.drawsBackground = true
        column2Header.headerCell.backgroundColor = NSColor.systemMint
        column2Header.headerCell.alignment = .center
        column2Header.headerCell.attributedStringValue = column2AttributedString
        column2Header.sizeToFit()
        column2Header.minWidth = 90.0
        column2Header.maxWidth = 90.0
        
        let scrollView = NSScrollView()
        scrollView.documentView = tableView
        
        return scrollView
    }
    
    func updateNSView(_ nsView: NSScrollView, context: Context) {
        let tableView = (nsView.documentView as! NSTableView)
        // work on this section
    }
}

The method you need to implement in your Coordinator is tableView(_:mouseDownInHeaderOf:).

Questions about AppKit are off-topic for these Swift forums. Apple will direct you to their own developer forums for such questions, but I recommend Stack Overflow.

Thank you. Works perfectly and easy to add.