How Do I Add Subviews to an NSTableHeaderCell?

684 views Asked by At

I have been experimenting with customizing NSTableViews and have gotten onto NSTableHeaderView and NSTableHeaderCell now.

I have a simple need. Each NSTableHeaderCell I want to plant my custom view onto. Which simply consists of two buttons.

Here is some code:

I have an NSTableHeaderCell category which simply changes the background color with and without highlight.

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    [[NSColor blueColor] setFill];
    NSRectFillUsingOperation(cellFrame, NSCompositeSourceAtop);
}

-(void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    [[NSColor greenColor] setFill];
    NSRectFillUsingOperation(cellFrame, NSCompositeSourceAtop);
}

Next, I have an NSViewController subclass, named NSTableHeaderViewController. This has a xib which I put my buttons into.

Lastly, in my main view, I set the NSTableHeaderView with specified custom height, and add my buttons view to it.

APTableHeaderViewController *tableHeaderVC = [[APTableHeaderViewController alloc] initWithNibName:@"APTableHeaderViewController" bundle:nil];

NSTableHeaderView *tableHeaderView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 120, 88)];

[tableHeaderView addSubview:tableHeaderVC.view];
[self.tableView setHeaderView:tableHeaderView];

This works partly as expected, creating my 120 pixel high table header with blue backgrounds, green on highlighting. However it only adds my buttons view, NSTableHeaderViewControlelr, once in the header at 0, 0.

I kind of expect this, but what is the difference between the header view and header cell?

Also, if I want my buttons view to be in each cell, how can I add it to each?

Thanks.

0

There are 0 answers