I'm not sure anymore, but it's possible that it had two rows at some point. Then I would have clicked a button that changed the data source array and the table view would have been reloaded with reloadData(). And in the end I clicked another button to add a new item to the data source array and insert the corresponding row in the table view, but since clickedRow was invalid, it crashed.
I have been using NSTableView for different apps for more than 10 years now so I have some experience with it, and the implementation in this case is quite simple. array is the one that can gets swapped whenever category changes, and otherArray is another one that is displayed when no category is selected.
func numberOfRows(in tableView: NSTableView) -> Int {
return array.isEmpty ? 0 : category.map({ array[$0]?.count ?? 0 }) ?? otherArray.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cell = MyTableCellView()
...
return cell
}
func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
...
}
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
...
}
func tableViewSelectionDidChange(_ notification: Notification) {
...
}