views:

153

answers:

2

I used this 'tutorial' to bind my array called 'collection' to a NSTableview on my interface: http://www.cocoadev.com/index.pl?NSArrayController

The interfacebuilder stuff isn't that hard. It becomes difficult when I try to actually show the data in my array into the view.

in my .h file:

@interface MyDocument : NSDocument
{
    NSMutableArray *collection;
    //other variables
}

and in my .m file:

@implementation MyDocument
@synthesize collection;
     //quite some functions

     inside one function (that works):
     [collection addObject:fileName];

     //some other functions

     inside the init function:
     collection = [[NSMutableArray alloc] init];

Now I guess the array is bound well to the interface and the tableview inside it, but ofcourse the tableview and its columns need to be filled in a specific way. Right now nothing shows after adding an item. with collection addObject:fileName function

Should I create a sub-Array as one item, filled with fields? And how should I bind these values/fields to the specific columns. (the fields are 'artist', 'title', etc)

I have already bound every column in Interface Builder to Array Controller with Controller key 'arrangedObjects' and Model Key Path 'artist','title',etc.

Please keep the explanation simple since I'm slowly starting to think I will never get this Array Controller thing... Objective-C doesn't seem that hard, but the binding which it needs is what I just don't get. Apple's examples are not sufficient to newbies

+1  A: 

Typically to populate your data you'd use a dictionary (the key would be the keyPath, and object the data) for each row, or even better, create a class to represent the data and create a new instance for each row. Bindings can be a little tricky at first (if you're new to Cocoa get used to the data source methods first), but have a look at this tutorial and the examples here. Both contain samples you can download and examine exactly how the bindings is set up in Interface Builder.

Marc Charbonneau
+1  A: 

Just mutating the array doesn't tell anything that the array has changed. You need to send KVO notifications for the mutation.

The right way to do this is to implement accessor methods for the property, then call your own accessors. In this case, you'll want to implement insertObjectInCollection:atIndex: and pass the length of the array as the index ([self insertObjectIntoCollection:fileName atIndex:[self countOfCollection], after also implementing countOfCollection).

When you implement accessors, then when an object binds to the property, Cocoa will wrap the accessors in KVO magic that will send the appropriate notifications for the mutation.

Peter Hosey