You should better open a new thread when you ask a new question.
You want a button in each cell ?
I suppose you create the TableView in IB.
Connect to an IBOutlet:
@IBOutlet weak var simpleTableView: NSTableView!
Then, in IB Identity Inspector, give an identifier to its Table Cell View "CellIdentifier"
Make the class that contains the TableView conform to NSTableViewDelegate, NSTableViewDataSource
At the load (could be appDidFinisLaunching), set the delegates
simpleTableView.delegate = self
simpleTableView.dataSource = self
Define the delegate and dataSource func.
Create the button in tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int)
// MARK:- TableView Delegate
func numberOfRows(in tableView: NSTableView) -> Int {
return 3 // The number of rows you have
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CellIdentifier"), owner: self) {
let button = NSButton(frame: NSRect(x: 10, y: 2, width: 80, height: 20)) // Take care that it fits in the cell
button.title = "Action"
button.target = self // The class where tableView is declared
button.action = #selector(buttonTest)
button.tag = row // That will allow to find which cell the button is in
(cellView as! NSTableCellView).textField?.stringValue = "Cell \(row + 1)"
cellView.addSubview(button)
return cellView
} else {
return nil
}
}
Complete the action to see the tag, and use it
@objc func buttonTest(_ sender: NSButton) {
print("Test button", sender.tag)
swicth sender.tag {
case 0: print("First cell clicked")
case 1: print("Second cell clicked")
case 2: print("Third cell clicked")
default : break
}
}