Re: FW: Enhancing NSTableView [LONG]
Re: FW: Enhancing NSTableView [LONG]
- Subject: Re: FW: Enhancing NSTableView [LONG]
- From: Stéphane Sudre <email@hidden>
- Date: Mon, 28 May 2001 12:56:25 +0200
On dimanche, mai 27, 2001, at 03:49 , Tom Waters wrote:
Set identifiers in your columns so you can find them after they've been
reordered.
cell = [[NSBrowserCell alloc] init];
[cell setLeaf: NO];
[[self tableColumnWithIdentifier: @"name"] setDataCell: cell];
then later in your delegate:
- (void)tableView:(NSTableView *)view
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)col
row:(int)row
{
if ([[col identifier] isEqualTo: @"name"]) {
[cell setImage: [self getTheRightImageForThisRow: row]];
}
}
I'll leave the implementation of getTheRightImageForThisRow up to
you... :-)
You should be able to extrapolate from here how to support and change
the values of other cells for the rest of your columns.
There is a big problem when you're using NSBrowserCell to do this. The
rightmost location of the cell is reserved for the leaf icon. So you
lose this space. That leads me to code this thing (the code is really
bad since I don't really understand NSCell but is working perfectly
well. And believe it or not, I don't know what the method new is doing.):
----- .h
#import <AppKit/AppKit.h>
#define MYPRIVATECELL_ICON_WIDTH 16
#define MYPRIVATECELL_ICON_HEIGHT 16
#define MYPRIVATECELL_INTERSPACE_WIDTH 3
#define MYPRIVATECELL_OFFSET 2
@interface MYPrivateCell : NSCell
{
NSDictionary * fontAttributes_;
}
- (void) setPrivateFont:(NSFont *) inFont;
@end
------- .m
#import "MYPrivateCell.h"
@implementation MYPrivateCell
- (void) setPrivateFont:(NSFont *) inFont
{
if (fontAttributes_!=nil)
{
[fontAttributes_ release]; // Default value is nil in the
constructror
}
fontAttributes_=[[NSDictionary dictionaryWithObject: inFont
forKey:NSFontAttributeName] retain];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView
*)controlView
{
NSImage * tImage=[self image];
NSString * tTitle=[self title];
[NSGraphicsContext saveGraphicsState];
[NSBezierPath clipRect:cellFrame];
if (tImage!=nil)
{
[tImage
compositeToPoint:NSMakePoint(cellFrame.origin.x+MYPRIVATECELL_OFFSET,cellFrame.
origin.y + (cellFrame.size.height - MYPRIVATECELL_ICON_HEIGHT) * 0.5 +
([controlView isFlipped] ? MYPRIVATECELL_ICON_HEIGHT : 0))
operation:NSCompositeSourceOver];
}
if (tTitle!=nil)
{
NSSize tFontSize=[tTitle sizeWithAttributes:fontAttributes_];
[[NSColor blackColor] set];
[tTitle
drawInRect:NSMakeRect(cellFrame.origin.x+MYPRIVATECELL_ICON_WIDTH+MYPRIVATECELL_INTERSPACE_WIDTH+
MYPRIVATECELL_OFFSET,cellFrame.origin.y+(cellFrame.size.height-
tFontSize.height)*0.5,tFontSize.width,tFontSize.height)
withAttributes:fontAttributes_];
}
[NSGraphicsContext restoreGraphicsState];
}
@end
in the awakeFromNib:
cell=[MYPrivateCell new];
[cell setPrivateFont:[NSFont systemFontOfSize:10.0]];
and in :
- (void)tableView:(NSTableView *)view willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *) inColumn row:(int) inRow
{
[...]
[cell setTitle:@"Mangez du Gloubiboulga"];
[cell setImage: [NSImage imageNamed:@"Casimir"]];
[...]
}