NSTableView(或 NSOutlineView)列通过上下文菜单隐藏:“无代码";解决方案 [英] NSTableView (or NSOutlineView) column hiding through contextual menu : a "no code" solution

查看:30
本文介绍了NSTableView(或 NSOutlineView)列通过上下文菜单隐藏:“无代码";解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个问题的最佳解决方案,找到了几个线索,但并不完全满足 IMO.所以我在我的无代码解决方案下面发布.目标是在 Finder 或其他本机应用程序中,在表格或大纲的标题栏上有一个上下文菜单,允许选择哪些列是可见的.

I searched SO for the best solution to this question, found several clues, while not fully satisfying IMO. So I post below my no-code solution. The goal is to have, as in Finder or other native applications, a contextual menu on the header bar of a table or outline, allowing to select which columns are visibles.

推荐答案

  1. 在 IB 故事板中,将您需要的所有列添加到表视图中.您可以将其中一些设置为默认隐藏.

  1. In IB storyboard, add all the columns you need into the table view. You may set some of them hidden by default.

Ctrl-将每一列从IB文档大纲视图拖到场景控制器代码视图中,以便为每一列自动创建弱IBOutlets.目的是能够以绑定中的给定列为目标.

Ctrl-drag each column from the IB document outline view to the scene controller code view, in order to automatically create weak IBOutlets for each column. The purpose is to be able to target a given column in bindings.

__weak IBOutlet NSTableColumn *my_column;

  • 向情节提要场景添加一个菜单,该菜单的项目数与您计划隐藏的列数相同.您不必为菜单项提供标题(请参阅下一点).

  • Add a menu to the storyboard scene with the same number of item as the columns you plan to hide. You do not have to give title to menu items (see next point).

    对于每个菜单项,在绑定窗格中,添加两个绑定:

    For each menu item, in the bindings pane, add two bindings :

    • 通过您的控制器出口将项目 title 绑定到列 title 属性:controller_name.my_column.title.这样,如果代码需要更改,菜单项将与列标题保持同步.
    • THIS ONE IS KEY:将菜单项的value 绑定到列hidden 属性:controller_name.my_column.hidden.添加一个 NSNegateBoolean 转换器以使菜单刻度有意义.
    • Bind the item title to the column title property through your controller outlet : controller_name.my_column.title. This way the menu item will stay in sync with column title, should the code need to change it.
    • THIS ONE IS KEY : Bind the value of the menu item to the column hidden property : controller_name.my_column.hidden. Add a NSNegateBoolean transformer for the menu tick to be meaningful.

    将菜单附加到标题菜单:按住 ctrl 键单击表格标题视图,并从菜单出口连接到在 #3 中创建的菜单.

    Attach the menu to header menu : ctrl-click on the table header view and make connection from the menu outlet to the menu created in #3.

    原来如此.除了 #2 中添加的 IBOutlets 之外没有任何代码.

    That it's. No code beside the IBOutlets added in #2.

    绑定有两种方式,取消勾选菜单项将隐藏绑定列.此外,如果代码隐藏了一列,绑定的菜单项将反映其状态.

    The bindings being two ways, un-ticking a menu item will hide the bound column. Also, if the code hides a column the bound menu item will reflect its state.

    这篇关于NSTableView(或 NSOutlineView)列通过上下文菜单隐藏:“无代码";解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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