Re: images in NSTableView
Re: images in NSTableView
- Subject: Re: images in NSTableView
- From: Graham Cox <email@hidden>
- Date: Mon, 12 May 2008 20:58:02 +1000
While it's probably slightly more work, you'd be better off writing a
custom NSCell subclass than trying to turn colours into images and
displaying them in a NSImageCell, IMO.
Here's part of one I did recently for colours - it's maybe more than
you need and it also takes an NSColor object value, rather than a
string, so you need to deal with that too (a dictionary could help you
map colour name -> colour object) but you'd have the exact same
problem using an image.
To use the custom cell class, at some sensible point in the
initialisation or set up (I use NSWindowController's windowDidLoad:
method, as the controller has an outlet to the table view that
contains it) do something like:
GCColourCell* cc = [[[GCColourCell alloc] init] autorelease];
[[mLayersTable tableColumnWithIdentifier:@"selectionColour"]
setDataCell:cc];
@implementation GCColourCell
#pragma mark As a GCColourCell
- (void) setColorValue:(NSColor*) colour
{
[colour retain];
[mColour release];
mColour = colour; // mColour is an ivar
}
- (NSColor*) colorValue
{
return mColour;
}
- (void) setState:(BOOL) state
{
mHighlighted = state; // mHighlighted is an ivar
}
#pragma mark -
#pragma mark As a NSCell
-(void) drawInteriorWithFrame:(NSRect) theFrame inView:(NSView*)
theView
{
#pragma unused(theView)
if([self colorValue] != nil )
{
NSRect r;
// could use NSColor's -drawSwatchInRect: here, but I don't want the
opacity cue
if ( mHighlighted )
[[NSColor darkGrayColor] set];
else
[[NSColor whiteColor] set];
r = NSInsetRect( theFrame, 6, 5 );
NSRectFill( r );
r = NSInsetRect( theFrame, 8, 7 );
[[self colorValue] set];
NSRectFill( r );
r = NSInsetRect( theFrame, 6, 5 );
[[NSColor darkGrayColor] set];
NSFrameRectWithWidth( r, 1 );
}
}
- (void) setObjectValue:(id) obj
{
if([obj isKindOfClass:[NSColor class]])
[self setColorValue:obj];
else
[self setColorValue:nil];
}
- (id) objectValue
{
return [self colorValue];
}
@end
On 12 May 2008, at 7:27 pm, Yann Disser wrote:
I need to display images in one column of a NSTable view. Right now,
the column contains values like "green", "red", "red green", "blue"
etc. I want to replace those strings with images of the
corresponding color(s) - in the case of "red green" by two images, a
red and a green one. I do not really understand, what I have to do
to accomplish this.
Thanks for your help,
Yann
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden