Simple NSTableView bindings example

2019-07-29 07:24发布

问题:

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

回答1:

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.