本文介绍了简单的NSTableView绑定示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在XCode中:

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

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

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

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.

在IB中:

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

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.

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

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

对于这两列,我将值绑定到AppController并设置ModelKeyPaths分别分配给NAME和VALUE。 Controllerp = ArrangedObjects。

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

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

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

推荐答案

NSArrayController不是那样的在存储方面与NSMutableArray不同。如果可以使用 [[[mutableArray firstObject] valueForKey:@ NAME]; 之类的东西取回所需的值,则数组控制器应该可以为您提供如果连接正确,则正确的值和绑定应该起作用。

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.

但是,如果上述代码没有为您提供所需的值,那么您将遇到其他问题。很难确定,但是现在,我将假设您有一组字典,它们的两个键都相同(我们叫它们 firstKey secondKey )。我将进一步假设目标是使第一列显示 firstKey 的所有值,第二列显示 secondKey code>。按照这种理论,您的AppDelegate中的NSMutableArray会保持上述状态,但是绑定将有所改变。

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.

首先,确保您正在运行的nib文件将文件的所有者设置为 AppDelegate

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

然后,将ArrayController Content Array绑定到 File的Owner.mutableArray

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.

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

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.

这篇关于简单的NSTableView绑定示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:20