How to Delete NSTableView Cell entry

Hi everyone. Swift noob here. I was following https://www.youtube.com/watch?v=dOskvb-fNgI since it’s really close to something I’m trying to accomplish. I don’t speak his language, but I was able to follow along with his code, and get it mostly working with Swift 5 and Xcode 11. The only bit I’m unable to figure out with Swift 5 is how to delete a selected table entry. It seems the code has changed enough to where I wasn’t able to “fix” it. He provides his source in the video description.


I was able to get the following to work, which obviously isn't what we want as it deletes row 0 every time user clicks the Delete button. But I'm hoping I'm on the right track.


    @IBAction func deleteButton(_ sender: NSButton) {
        self.data.remove(at: 0)
        tableView.reloadData()


If anyone has suggestions on that last piece I’d be very grateful.


Thanks!

Replies

Simply do this


@IBAction func deleteButton(_ sender: NSButton) {
    if let row = tableView.indexPathForSelectedRow {
      self.data.remove(at: row)
      tableView.reloadData()
    }
}