views:

37

answers:

3

I want to put a custom control inside the view for my NSCollectionViewItem.

Lets say I have a custom NSView called BoxesView.BoxesView is just a view that draws a predetermined number of boxes in its view. That number of boxes is set in the init method. Lets say I set it to 8.

When I load up the collection view, all the other controls in the view work fine, (buttons, sliders, etc.) but my view won't draw.

If I set a breakpoint in the drawRect method of BoxesView it shows that the number of boxes to draw is 0! If I set a breakpoint in my init method where I set numBoxes to 8, it shows that numBoxes does actually get set to 8. Also, the init method only gets called 1 time even though there are multiple rows in the collection view.

What am I doing wrong?

UPDATE

I was able to get this working by setting the itemPrototype to load from a xib instead of being in the same xib as the NSCollectionViewItem. This is great, except it only works on 10.6 and not 10.5.

UPDATE 2

What I'm trying to do, is stick my custom view inside the view that already existed for the NSCollectionViewItem that already exists. What happens is the member variable mBoxWidth gets blown away and is zero so when it goes to draw it, nothing happens.

@implementation DumbView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       mBoxWidth = 3;
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
   NSRect bounds = self.bounds;
   [[NSColor redColor]set];
   [NSBezierPath fillRect:NSMakeRect(bounds.origin.x, bounds.origin.y, mBoxWidth, mBoxWidth)];
}

@end

A: 
Joshua Nozzi
What the problem ends up being is that the default values that I'm giving to my member values are all zeroed out. So any booleans are NO, any floats are 0.0, and everything else is 0.I tried doing what you said and I end up in the same place.
Randall
Then I'd suggest adding as much additional detail to your question as possible. It's just not clear what you're doing.
Joshua Nozzi
A: 

Have you tried overloading copyWithZone?

I'm guessing your item is getting copied and not directly init'd.

JimDusseau
A: 

I didn't implement initWithCoder. That fixes everything.

Randall