获取焦点行的 NSTableView 行索引?

Getting NSTableView row index of focussed row?

如何获取 NSTableView 中当前正在聚焦的行的索引?当左键单击该行时,该行被选中,但当该行被右键单击时我需要索引,即当用户在其上打开上下文菜单但该行确实获得焦点时,该行可能未被选中。我在文档中找不到有关如何执行此操作的任何详细信息。

您可以从tableView.clickedRow获取右击行的索引。注意,如果用户右击选中(高亮)行,系统会"focus" 全部选中行(前提是有多个选中行;你可以在发现者)。所以你可能也想考虑到这一点。

可能的实现:

int clickedRow = tableView.clickedRow;
NSIndexSet *focusedIndexes;
if (clickedRow != -1) {
    if ([tableView isRowSelected:clickedRow]) {
        focusedIndexes = tableView.selectedRowIndexes;
    } else {
        focusedIndexes = [NSIndexSet indexSetWithIndex:clickedRow];
    }
    // do what you desire with focusedIndexes
}