RE: Placing controls inside a table header view? (Solution)
RE: Placing controls inside a table header view? (Solution)
- Subject: RE: Placing controls inside a table header view? (Solution)
- From: Keith Blount <email@hidden>
- Date: Sat, 16 Apr 2005 07:37:38 -0700 (PDT)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Well, I solved this myself. In case anybody searches
the archives with a similar problem, I'll post my
solution.
The problem: how to use a table header view above an
arbitrary view (like a text view), just as Xcode does
above its editor, containing popup menus and buttons
for navigation.
The solution:
In IB, set up a table view that is shrunk down so that
only its header view is visible and place it above the
text view (I got this method from looking at how
DevonThink does it). Uncheck everything apart from
Empty Selection and Column Headers. Set up identifiers
for the columns and assign a delegate for the table
view.
For plain old buttons, just call setImage: on the
column headerCell. Then in the table view's delegate,
use -tableView:didClickTableColumn: to find out if the
column header has been clicked, and perform the
action.
For popup buttons in the header view, I created a
custom cell class, as follows:
@interface KBPopUpTableHeaderCell : NSPopUpButtonCell
{
NSTableHeaderCell *tableHeaderCell; // Just used for
drawing the background
NSImage *arrows;
}
@end
@implementation KBPopUpTableHeaderCell
- (id)init
{
if (self = [super init])
{
// Init our table header cell and set a blank title,
ready for drawing
tableHeaderCell = [[NSTableHeaderCell alloc] init];
[tableHeaderCell setTitle:@""];
// Set up our custom arrows image
arrows = [[NSImage imageNamed:@"arrowstest.tif"]
retain];
// Set up the popup cell attributes
[self setControlSize:NSMiniControlSize];
[self setArrowPosition:NSPopUpNoArrow];
[self setBordered:NO];
[self setBezeled:NO];
[self setFont:[NSFont systemFontOfSize:[NSFont
smallSystemFontSize]]];
}
return self;
}
- (void)dealloc
{
[tableHeaderCell release];
[arrows release];
[super dealloc];
}
// We do all drawing ourselves to make our popup cell
look like a header cell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView
*)controlView
{
/*
* Use our table header cell to draw the background
of the popup cell so that
* it looks like a table header. This method draws
the cell in the currently
* focused view, and the docs say not to take
advantage of this, but seeing as in
* this case the currently focused view and
controlView are one and the same, we
* should be okay. Unfortunately, there is no other
way to draw the background of
* a cell in the table header style (other than using
an image).
*/
[tableHeaderCell drawWithFrame:cellFrame
inView:controlView];
if (cellFrame.size.width > 0)
{
// Draw our own popup arrows on the right
NSSize size = [arrows size];
NSPoint p;
p.x = NSMaxX(cellFrame) - size.width - 4.0; //
Arrows are drawn 4 pixels in from right
p.y = (cellFrame.size.height - size.height)/2;
[arrows drawAtPoint:p
fromRect:NSMakeRect(0,0,size.width,size.height)
operation:NSCompositeSourceOver
fraction:1.0];
}
// Now draw the text and image over the top
[self drawInteriorWithFrame:cellFrame
inView:controlView];
}
@end
Create an instance of this class and call
setHeaderCell: on the table column whose header should
contain a popup. Then, in
-tableView:didClickTableColumn:, do something like
this:
- (void)tableView:(NSTableView *)tableView
didClickTableColumn:(NSTableColumn *)tableColumn
{
NSString *identifier = [tableColumn identifier];
NSTableHeaderView *headerView = [tableView
headerView];
id headerCell;
if ([identifier isEqualToString:@"ID"])
{
headerCell = [tableColumn headerCell];
NSRect rect = [headerView
headerRectOfColumn:[tableView
columnWithIdentifier:identifier]];
[headerCell performClickWithFrame:rect
inView:headerView];
[tableColumn setWidth:[headerCell cellSize].width];
[tableView sizeLastColumnToFit];
}
}
Hope that's of some use to somebody else at some
point.
Cheers,
Keith
__________________________________
Do you Yahoo!?
Plan great trips with Yahoo! Travel: Now over 17,000 guides!
http://travel.yahoo.com/p-travelguide
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden