简单的 NSTableView 绑定示例

发布于 2024-09-03 06:11:30 字数 692 浏览 1 评论 0原文

我试图通过绑定填充 2 列 NSVTableView,但数据未显示在表中。

在 XCode 中:

我的 AppDelegate 中有一个 NSMutableArray 来保存数据,以及相应的 @property 和 @synthesize。

在一个事件中,我在 NSMutableArray 上调用 [removeAllObjects] 并用一些 NSDictionary 对象重新填充它。每个字典包含 2 个 KVP:一个 NAME 和一个 VALUE。

在 IB 中:

我添加了一个 NSArrayController 并将其内容数组绑定到我的 AppDelegate,并将 ModelKeyPath 设置为 AppDelegate 中 NSMutableArray 的名称。

在 NSTableView 上,我将内容绑定到 ArrayController。 ControllerKey =“arrangedObjects”,ModelKeyPath =

对于两列中的每一列,我将 Value 绑定到 AppController 并将 ModelKeyPaths 分别设置为 NAME 和 VALUE。 ControllerKey =“排列对象”。


我尝试了其他几件事,例如在应用程序委托中使用 NSArray 并在每次需要更新值时创建一个新的。一定有一些小事我忘记了。它是什么?

约吉

I am trying to populate a 2-column NSVTableView via bindings, but the data is not showing up in the table.

In XCode:

I have a NSMutableArray in my AppDelegate that will hold the data, as well as the corresponding @property and @synthesize.

On an event, I call [removeAllObjects] on the NSMutableArray and repopulate it with some NSDictionary objects. Each dictionary contains 2 KVP's: a NAME and a VALUE.

In IB:

I added an NSArrayController and bound it's Content Array to my AppDelegate and set the ModelKeyPath to the name of the NSMutableArray in the AppDelegate.

On the NSTableView, I bound the Content to the ArrayController. ControllerKey = "arrangedObjects", ModelKeyPath = empty.

For each of the two columns, I bound the Value to the AppController and set the ModelKeyPaths to NAME and VALUE respectively. ControllerKey = "ArrangedObjects".


I have tried several other things, such as using an NSArray in the app delegate and making a new one every time I need to update the values. There must be some tiny little thing I am forgetting. What is it?

jorj

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

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

发布评论

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

评论(1

凉薄对峙 2024-09-10 06:11:30

NSArrayController 在存储方面与 NSMutableArray 没有太大区别。如果您可以使用类似 [[mutableArray firstObject] valueForKey:@"NAME"]; 并获取您需要的值,那么数组控制器应该能够为您提供正确的值,并且绑定应该如果你有正确的联系,就可以工作。

但是,如果上述代码行没有为您提供所需的值,那么您就会遇到不同的问题。很难确定,但现在我假设您有一个字典数组,它们都具有相同的两个键(我们称它们为 firstKeysecondKey) 。我将进一步假设目标是让第一列显示 firstKey 的所有值,第二列显示 secondKey 的值。根据这个理论,您的 AppDelegate 中的 NSMutableArray 保持您上面描述的方式,但是您的绑定会发生一些变化。

首先,确保您正在使用的 nib 文件的文件所有者设置为 AppDelegate

然后,ArrayController 内容数组应绑定到File's Owner.mutableArray

表视图的整体绑定并不是非常重要,因为我只是绑定列没有遇到任何问题。

第一列的 Value 应绑定到 ArrayController.arrangedObjects.firstKey 。当然,第二列是相同的,只是模型键路径是 secondKey

The NSArrayController is not that different from an NSMutableArray in terms of storage. If you can use something like [[mutableArray firstObject] valueForKey:@"NAME"]; and get back the value you need then the array controller should be able to supply you with the correct value and bindings should work if you have the right connections.

If, however the aforementioned line of code does not give you the value you need then you have a different problem. It is hard to tell for sure but I will, for now, assume you have an array of dictionaries that all have the same two keys (let's call them firstKey and secondKey). Ii will further assume that the goal is to have the first column show all of the values for firstKey and the second column show the values for secondKey. Under this theory the NSMutableArray in your AppDelegate stays the way you described above, your bindings, however, will change a bit.

First, make sure the nib file you are working with has it's File's Owner set to AppDelegate.

Then, the ArrayController Content Array should be bound to File's Owner.mutableArray.

The table view's overall binding is not terribly important as I have not had any issues just binding the columns.

The first column's Value should be bound to ArrayController.arrangedObjects.firstKey. The second column is, of course, the same except the model key path is secondKey.

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