views:

407

answers:

1

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  A: 

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.

theMikeSwan
I have everything exactly as you describe. I looked a little harder and discovered that I was initializing "mutableArray" in -(void)awakeFromNib. When I moved that (and the adding of some test data) to -(id)init, I saw the test data in the table. When I call [mutableArray removeAllObjects] and repopulate it with data, do I have to call something to tell the array controller to resync the table?jorj
sirjorj
I normally use array controllers with Core Data, but changing the mutable array directly is likely going behind the controller's back so it doesn't know it needs to refresh the data. Try sandwiching these two lines of code around any changes you make to `mutableAray` `[self willChangeValueForKey:@"mutableArray"];` and `[self didChangeValueForKey:@"mutableArray"];`. If those methods are not available to your `AppDelegate` try making your changes through the NSArrayController and just use `mutableArray` for archiving.
theMikeSwan
the willChange... and didChange... fixed it! Thank you much!
sirjorj

related questions