views:

393

answers:

1

I'm trying to create an NSMatrix of an NSImageCell prototype programmatically (if you can help me doing it via Interface Builder, be my guest at answering me here)

I created a new, empty project. In it, a normal object (subclassing NSObject) has this code (and only this code):

- (void) awakeFromNib {
    NSRect f = view.bounds;
    NSMatrix* matrix = [[NSMatrix alloc] initWithFrame:f mode:NSListModeMatrix cellClass:[NSImageCell class] numberOfRows:1 numberOfColumns:1];

    [matrix setCellSize:NSMakeSize(100, 100)];
    [matrix sizeToCells];
    [matrix setNeedsDisplay:YES];

    [view addSubview:[matrix autorelease]];

    [view setNeedsDisplay:YES];

    NSLog(@"Matrix frame: %@", NSStringFromRect(matrix.frame));
}

"view" is an NSView specified as an outlet, connected to the Content View of the Window in Interface Builder.

The matrix does not show at all. It prints to log that the frame size is (0,0,100,100), as expected.

What am I doing wrong?

As the title states, I'm doing this on a Snow Leopard machine, using Xcode 3.2.1.

A: 

The NSMatrix doesn't draw anything by default as it's a container for the NSCells in it. You can verify that your NSMatrix is correctly added to the view by setting it to draw it's background.

[matrix setBackgroundColor:[NSColor blueColor]];
[matrix setDrawsBackground:YES];

Since the NSMatrix is a container for NSCell you need to fill them with something. In the example you posted you can do this by fetching the cell corresponding to your only row and column and setting the image.

NSImageCell *cell = (NSImageCell *)[matrix cellAtRow:0 column:0];
[cell setImage:[NSImage imageNamed:@"image.jpg"]];

Also, keep in mind that your matrix is only big enough to show one cell and as you add columns and rows to it you need to increase its size.

m5h
Once I set the cell size (as in my example) shouldn't it at least show empty NSImageCells?
Aviad Ben Dov
The NSImageCell doesn't draw anything either unless you set an image in it.
m5h