Chris
Chris

Reputation: 1015

Custom background image on NSTableViewCell

I have a view based tableview with a custom cell with a bunch of labels and an image on it. I want to add a background image to each cell. First I tried adding an imageview to the cell but that covers up the selection highlight. I then tried to set the background of the tableview but the image moves when resizing the view. How can I do this?

Upvotes: 2

Views: 669

Answers (1)

Chris
Chris

Reputation: 1015

Thanks to boyfarrell I was able to figure it out. I did as he suggested and subclassed NSTableRowView and added the background in drawBackgroundInRect: like this:

- (void)drawBackgroundInRect:(NSRect)dirtyRect
{    
    NSImage *backgroundImage = [NSImage imageNamed:@"plaincellbg.png"];
    [backgroundImage drawInRect:self.bounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
}

The image fills the cell and stretches when the window is resized.

Upvotes: 1

Related Questions