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!

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hooligancat
  • 3,588
  • 1
  • 37
  • 55
  • What do you mean by “this just creates the first object image”? As far as I can see, you're doing everything right. (I would use a synthesized property instead of explicitly-defined accessors for the cell's model-object property, though. Also, it already has an `objectValue` property.) – Peter Hosey Aug 11 '10 at 09:53
  • @Peter - thanks for replying. In my statement "this just creates the first object image" I actually meant only cellOne (from my example) is drawn into the matrix. cellTwo never gets drawn. It is almost as if there is only one row and one column in the matrix. I have confirmed that the matrix is the right size by logging out the size (and set a background color to be sure). My rows and columns are defined in the initializer so I know they are being set. As you pointed out I it *appears* to be right. So frustrating! – Hooligancat Aug 11 '10 at 16:13

1 Answers1

0

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]

agf
  • 171,228
  • 44
  • 289
  • 238
ortnec
  • 327
  • 2
  • 5