custom cell drawing
custom cell drawing
- Subject: custom cell drawing
- From: Nick <email@hidden>
- Date: Sat, 01 Mar 2003 12:05:05 -0500
Hello, folks.
Have an interesting and annoying problem.
I have inherited NSCell to do some custom drawing in a NSTableView. The
text renders fine, however, the images do not. Every single cell has an
image and a little bit of text. However, after the first row, the
images will not render in the NSTableView.
Here's the code:
/* .h file */
#import <Cocoa/Cocoa.h>
@interface PHBAccountsView : NSTableView
{
}
@end
@interface TrippleTextCell : NSCell
{
@private
NSString * accountType;
NSString * bank;
NSString * accountName;
NSString * balance;
}
@end
/* .m file */
#define LargeTextAttributes [[NSDictionary alloc]
initWithObjectsAndKeys: [NSFont boldSystemFontOfSize: 12],
NSFontAttributeName, [NSColor blackColor],
NSForegroundColorAttributeName, nil]
#define SmallTextAttributes [[NSDictionary alloc]
initWithObjectsAndKeys: [NSFont systemFontOfSize: 10],
NSFontAttributeName, [NSColor colorWithCalibratedRed: 0.0 green: 0.0
blue: 0.0 alpha: 0.8 ], NSForegroundColorAttributeName, nil]
#define MoneyTextAttributes [[NSDictionary alloc]
initWithObjectsAndKeys: [NSFont systemFontOfSize: 10],
NSFontAttributeName, [NSColor colorWithCalibratedRed: 0.14 green: 0.50
blue: 0.19 alpha: 0.8 ], NSForegroundColorAttributeName, nil]
@implementation TrippleTextCell
- (void) setObjectValue: (NSObject *) object
{
accountType = [object objectAtIndex: 2];
bank = [object objectAtIndex: 1];
accountName = [object objectAtIndex: 0];
balance = [[object objectAtIndex: 3] stringValue];
}
- (void) drawWithFrame: (NSRect ) frame inView: (NSView *) view
{
NSPoint p;
NSRect r;
NSString * say;
NSImage * im;
im = nil;
[super drawWithFrame: frame inView: view];
//determine what image depending on the accountType
im = [NSImage imageNamed: accountType];
p = frame.origin;
r = frame;
r.size.width= 39;
r.size.height = 39;
[im setSize: r.size];
[im setScalesWhenResized: NO];
p.x += 2;
p.y += 2;
[im drawAtPoint: p fromRect: r operation: NSCompositeSourceAtop
fraction: 1.0];
p.x += 45;
p.y += 0;
say = accountName;
[say drawAtPoint: p withAttributes: LargeTextAttributes];
say = bank;
p.x += 10;
p.y += 15;
[say drawAtPoint: p withAttributes: SmallTextAttributes];
say = balance;
p.y += 13;
[say drawAtPoint: p withAttributes: MoneyTextAttributes];
}
@end
@implementation PHBAccountsView
- (void) awakeFromNib
{
NSTableColumn * ntc;
TrippleTextCell * ttc;
[self setRowHeight: 42];
ttc = [[[TrippleTextCell alloc] init] autorelease];
while([self numberOfColumns] > 0)
{
[self removeTableColumn: [[self tableColumns] objectAtIndex:0]];
}
ntc = [[[NSTableColumn alloc] initWithIdentifier: @"Accounts"]
autorelease];
[[ntc headerCell] setStringValue:@"Accounts"];
[ntc setDataCell: ttc];
[self addTableColumn: ntc];
}
@end
Thanks a lot for the help. I really cannot figure it out.
Nick
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.