如何使用绑定在表视图中显示按钮单元格(复选框)标题

发布于 2024-12-14 11:46:26 字数 941 浏览 2 评论 0原文

我正在尝试一个简单的应用程序,其中有一个可变字典的可变数组,例如 -

NSMutableDictionary *sample6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"title6",@"title",[NSNumber numberWithBool:NO],@"state", nil];

在 IB 中,我使用 NSButtonCell (复选框)创建了一个表视图。

我能够使用下表列绑定来显示复选框状态(选中或未选中):

Value - ArrayController.arrangedObjects.state

在本例中,它显示标题为“Check”的复选框数组,如下面的屏幕截图所示:

在此处输入图像描述

现在我的目标是使用绑定显示复选框标题,这样它 从相同的可变字典中获取值 状态。

我尝试了以下纽扣电池绑定,但它不起作用:

标题-> ArrayController.selection.title

我还尝试了按钮单元格的绑定:

标题-> ArrayController.arrangedObjects.title

但它不起作用,使用上面的绑定后看起来像这样:

在此处输入图像描述

< em>有人可以建议我使用哪个控制器键吗?如果这不是显示标题的正确方法,那么正确的方法是什么?

I am trying a simple application where I have a mutable array of mutable dictionaries, such as -

NSMutableDictionary *sample6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"title6",@"title",[NSNumber numberWithBool:NO],@"state", nil];

In IB I created a table view with NSButtonCell (check box).

I was able to show checkboxes state (checked or unchecked), using following table column bindings:

Value - ArrayController.arrangedObjects.state

In this case it shows an array of checkboxes with title - "Check" as shown in below screen-shot:

enter image description here

Now my aim is to show checkboxes title using bindings, such that it
gets value from same mutable dictionary from which it is getting its
state.

I tried following binding for button cell but it did not work:

title -> ArrayController.selection.title

I also tried this binding for button cell :

title -> ArrayController.arrangedObjects.title

but it didn't work, it appeared like this after using above binding:

enter image description here

Can any one suggest me which controller key to use and if this is not the correct way to show titles then what is the correct way to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-12-21 11:46:27

不幸的是,如果您想这样做,则需要编写一些代码。将表列值绑定到数组时,表列将处理获取原型数据单元格、设置其值并为每行“标记”到位。按钮单元格的绑定不会“通过”表格列公开,因此简单的绑定无法为您完成此操作。

回答您的问题

所以。由于仅公开值绑定,因此如果您确实希望复选框的标题反映该值(即,您确实希望复选框既处理选中状态又显示标题),则必须手动设置标题。为此,您必须将绑定与 混合使用。 NSTableDelegateProtocol > >。使用 -tableView:willDisplayCell:forTableColumn:row: 方法设置每次询问时,将单元格的 -title 属性更改为数组控制器的 -arrangedObjects 数组中正确对象的属性。对于最基本的应用程序来说,混合绑定和数据源/委托方法实际上很常见,因此不必担心您正在做一些肮脏的事情。注意:您将无法支持通过执行此操作来编辑标题,因为它是一个复选框。

替代设计

就我个人而言,我会避免所有这些,只为标题添加一个单独的表格列。将新列的值绑定到数组控制器的 arrangedObjects.title 并关闭复选框按钮单元格的标题,以便仅显示复选框本身。这极大地简化了整个过程并允许编辑标题。

Unfortunately you'll need to write a little code if you want to do it this way. When binding table column values to an array, the table column is handling taking the prototype data cell, setting its values, and "stamping" it in place for each row. The button cell's bindings aren't exposed "through" the table column, so a simple binding won't do it for you.

To Answer Your Question

So. Since only the value binding is exposed, the title must be set manually if you really want the checkbox's title to reflect the value (ie, you really want the checkbox to handle both the check state and displaying the title). To do this, you have to mix bindings with < NSTableDelegateProtocol >. Use the -tableView:willDisplayCell:forTableColumn:row: method to set the cell's -title property to that of the proper object in your array controller's -arrangedObjects array each time you're asked. Mixing bindings and data source / delegate methods is actually quite common for more than the most basic applications, so don't worry that you're doing something dirty. Note: you won't be able to support editing the title by doing this since it's a checkbox.

An Alternative Design

Personally, I'd avoid all that and just add a separate table column for the title. Bind the new column's value to the array controller's arrangedObjects.title and turn off the checkbox button cell's title so only the checkbox itself is displayed. That simplifies the whole thing greatly and allows editing the title.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文