views:

40

answers:

1

I have an NSArray of custom NSObjects. Each object has some properties and an image that I would like to display in a grid view. NSMatrix appears to be a good solution to my problem, but I am having issues getting the content of the objects to display.

Couple of things to note.

  1. I am not using core data
  2. I am trying to do this programmatically
  3. I have considered using NSCollectionView but NSMatrix appears to be a better solution in this case
  4. All the cells follow the same display format as each other - i.e. I'm not wanting to pass different cells different types of objects, just a different instance of the object

Assume I have an NSView (matrixContainerView) in a window. The controller file has an IBOutlet to matrixContainerView. In my controller I have the following in my awakeFromNib:

    NSMatrix* matrix = [[NSMatrix alloc] 
                          initWithFrame:[matrixContainerView bounds]
                                   mode:NSRadioModeMatrix 
                              cellClass:[MyCustomCell class] 
                           numberOfRows:5
                        numberOfColumns:5];

    [matrix setCellSize:NSMakeSize(116, 96)];
    [matrix setNeedsDisplay:YES];

    [matrixContainerView addSubview:[matrix autorelease]];

    [matrixContainerView setNeedsDisplay:YES];

The class MyCustomCell header looks like the following:

@interface MyCustomCell : NSCell {

    MyModel * theObject;

}

-(MyModel *)theObject;
-(void)setTheObject:(MyModel *)newValue;

And the implementation file as follows (drawing simplified):

@implementation MyCustomCell

-(void)drawInteriorWithFrame:(NSRect)theFrame inView:(NSView *)theView {

    ...drawing code using MyModel e.g. [MyModel isValid] etc...

}

-(MyModel *)theObject {
    return theObject;
}

-(void)setTheObject:(MyModel *)newValue {
    [theObject autorelease];
    theObject = [newValue retain];
}

@end

After some initialization and population of the array containing MyModel objects in the controller, I want to populate the NSMatrix with instances of the objects.

How do I do this?

I have tried adding just two objects from the array as follows (just as a test):

MyCustomCell * cellOne = (MyCustomCell *)[matrix cellAtRow:0 column:0];
[cell setTheObject:[myArrayOfObjects objectAtIndex:0]];

MyCustomCell * cellTwo = (MyCustomCell *)[matrix cellAtRow:0 column:1];
[cellTwo setTheObject:[myArrayOfObjects objectAtIndex:1]];

But this just creates the first object image. If the above had worked, it would been a straightforward task of enumerating through the array and adding the objects.

How do I go about adding the cells and passing the appropriate objects to those cells in order that they can be displayed correctly?

The Apple docs are sparse to say the least on NSMatrix as far as the programming guide goes. The information in there is very useful to me, but only after I have added the objects and got them displaying!

As usual any and all help much appreciated.

Update

If I do not add the two objects (as per my example above) the output is no different, i.e. a single representation of my custom cell is drawn to screen. This tells me that the single representation I see is being done at the initialization of the matrix and in fact I wasn't drawing anything to column 0 row 0 when in fact I thought I was. Which leaves me now more confused.

A: 

Might be that the matrix actually has the two cells but its frame is too small to display them? After adding the cells try calling [matrix sizeToCells]

ortnec