防止在基于NSTableView的视图中的特定单元格上显示上下文菜单 [英] Preventing contextual menu showing on specific cell in a view based NSTableView

查看:63
本文介绍了防止在基于NSTableView的视图中的特定单元格上显示上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于视图的NSTableView中的特定单元格上右键单击时,有什么方法可以阻止显示上下文菜单(以及单元格视图周围的相关选择环") ?

Is there any way of preventing a contextual menu (and the associated selection "ring" around the cell view) being shown when right-clicking on a specific cell in a view-based NSTableView ?

我并不是说要在所有单元格上禁用右键单击操作,而只是在特定单元格上禁用.

I'm not talking about disabling the right-click action on ALL the cells, but only on specific ones.

我显然已经尝试了所有处理选择更改的委托方法,但是没有作用,因为selectedRow属性没有变化,只有clickedRow发生了变化. 所以基本上我正在寻找与

I've obviously tried all the delegate methods dealing with selection changes but none works because the selectedRow property is not changing, only the clickedRow does. So basically I'm looking for something equivalent to

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool 

,但对于已点击行,而不是已选择行.

but for the clicked row not the selected row.

注意:问题是关于 macOS 上的NSTableView而不是iOS上的UITableView.

Note: the questions is about NSTableView on macOS and not the UITableViewon iOS.

推荐答案

我发现了一种可以做自己想做的方法,尽管看起来有些复杂,但应该做得更简单.因此,我欢迎任何更简单的解决方案.

I've found a way to do what I wanted, although looks like a little to involved for something that should be simpler. So I welcome any simpler solution.

可以通过将NSTableView子类化来完成:

It can be done by subclassing NSTableView :

class MyTableView : NSTableView {

    override func menu(for event: NSEvent) -> NSMenu? {
        let clickedPoint = self.convert(event.locationInWindow, from: nil)
        let row = self.row(at: clickedPoint)

        // no contextual menu for the last row
        return row == self.numberOfRows - 1 ? nil : super.menu(for: event)
    }
} 

此示例阻止在最后一行显示上下文菜单,但是可以通过添加具有方法以返回每个单元格菜单的委托来实现更通用的解决方案.

This example prevents the contextual menu to be shown for the last row, but a more generic solution could be implemented by adding a delegate with a method to return the menu for each cell.

这篇关于防止在基于NSTableView的视图中的特定单元格上显示上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆