Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > problem of NSPopUpButtonCell within NSTableHeaderView

problem of NSPopUpButtonCell within NSTableHeaderView
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2006
Status: Offline
Reply With Quote
Aug 3, 2006, 08:00 PM
 
Hi all,

My problem is that I can't get NSPopUpButtonCells to appear correctly
within an NSTableHeaderView. I'm trying to put popup buttons in my
column headers. I click on the header, and the mouseDown event fires in
my class which I subclassed from NSTableHeaderView, but the menu
doesn't popup so that I can select a new column header. I'm not really
sure what I need to be doing in the mouseDown event though (I was told
I needed to subclass my NSTableHeaderView to make it work, but still no
go, although I do get a mouseDown event now).

Another problem is that I have four columns and four headers of which I
can confirm in my code, but there are 5 headers that appear (the fourth
is shown twice as if it's not being redrawn).

Using IB, I have an NSTableView inside an NSScrollView and w/ the
inspector I have '0 Columns' (because I build the columns
programatically) and 'Column Headers Displayed' is checked.

Anyone have any ideas as to why my table headers are not working? I
can't see what's wrong, or really just don't understand what else I
need to be doing to make this work.

This is my table controller ...
Code:
- (void) awakeFromNib { NSTableHeaderView *currentTableHeader = [tableView headerView]; DataTableHeader *dataTableHeader = [[DataTableHeader alloc] init]; [dataTableHeader setFrame:[currentTableHeader frame]]; [dataTableHeader setBounds:[currentTableHeader bounds]]; [tableView setHeaderView:dataTableHeader]; }
Here I build the columns ...
Code:
NSTableColumn *column; NSPopUpButtonCell *headerCell; ... headerCell = [[NSPopUpButtonCell alloc] initTextCell:col]; [headerCell setControlSize:NSMiniControlSize]; [headerCell setBordered:NO]; [headerCell setEditable:YES]; [headerCell setPullsDown:NO]; [headerCell setFont:[NSFont labelFontOfSize: [NSFont systemFontSize]]]; [headerCell addItemWithTitle:@"itemA"]; [headerCell addItemWithTitle:@"itemB"]; [headerCell addItemWithTitle:@"itemC"]; column = [[NSTableColumn alloc] initWithIdentifier:[NSNumber numberWithInt:i]]; [column setHeaderCell:headerCell]; [column sizeToFit]; [tableView addTableColumn:column]; ...
I subclassed NSTableHeaderView w/ my new class but don't really know
what else I should be doing here.

Code:
@interface DataTableHeader : NSTableHeaderView { } - (void)mouseDown:(NSEvent *)theEvent; @end @implementation DataTableHeader - (void)mouseDown:(NSEvent *)theEvent { NSLog(@"mouseDown event"); [self setNeedsDisplay:YES]; }
Thanks,
     
Dedicated MacNNer
Join Date: Jun 2006
Status: Offline
Reply With Quote
Nov 15, 2007, 12:00 PM
 
Everywhere I looked for this answer, all I found were variations of this question. For the record, and to spare the next poor soul, here is what I discovered:

First, what do you need to do once you capture the mouse down event?

- (void)mouseDown:(NSEvent *)theEvent
{
// Figure which column, if any, was clicked
NSPoint clickedPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger columnIndex = [self columnAtPoint:clickedPoint];
if (columnIndex < 0)
return [super mouseDown:theEvent];

NSRect columnRect = [self headerRectOfColumn:columnIndex];

// I want to preserve column resizing. If you do not, remove this
if (![self mouse:clickedPoint inRect:NSInsetRect(columnRect, 3, 0)])
return [super mouseDown:theEvent];

// Now, pop the cell's menu
[[[self.tableView.tableColumns objectAtIndex:columnIndex] headerCell] performClickWithFrame:columnRect inView:self];
}

Then, to make this actually draw correctly, we need to re-override the opacity of the view so it doesn't expect us to draw ourselves.

- (BOOL)isOpaque;
{
return NO;
}

...and there you have it. NSTableView with NSPopUpButtonCell headers. I've only tested this on Leopard, of course, but I imagine it's about the same on Tiger.

Mike Lee, Majordomo
Delicious Monster Software
(Last edited by Delicious Monster; Nov 15, 2007 at 12:02 PM. (Reason:Fix whitespace))
     
Fresh-Faced Recruit
Join Date: May 2008
Status: Offline
Reply With Quote
May 25, 2008, 05:00 AM
 
I tried the above code and it all draws and responds well to mouse clicks as expected. However, when I do click on the popUpHeader and select, say, the 'Foo 4' item, when the mouse is released, the popUpHeader display always remains on the first menu item rather than the selected one.

I have been trying to figure out why this is so, but with no solutions.
     
Fresh-Faced Recruit
Join Date: May 2008
Status: Offline
Reply With Quote
May 26, 2008, 02:20 PM
 
Figured it out:

after the line of code below, you need to display
// Now, pop the cell's menu
[[[self.tableView.tableColumns objectAtIndex:columnIndex] headerCell] performClickWithFrame:columnRect inView:self];

[self setNeedsDisplay:YES];
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:56 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2