Skip navigation.
 
mlColorCell, NSActionCell subclass
FROM : Carl Gieringer
DATE : Mon Apr 18 07:53:52 2005

Hello,

I have not found this question definitively answered in the archives or
via google, but I apologize if I missed something.

I am trying to create a subclass of NSActionCell that displays a color.
  Clicking on the cell presents the color panel, which allows the user
to change the color of the cell.  I am having a problem propagating the
selection of a color back to the object which is "represented" by the
cell.  In particular, I do not understand how to get an NSTableView to
call -tableView:setObjectValue:forTableColumn:row: on its dataSource
when I am using this custom cell in one of the NSTableView's columns. 
I thought that calling -setObjectValue on my cell would suffice, but it
doesn't.

My implementation follows; thanks!

- (id) init
{
   if ( self = [super init] ) {
       
       [self setEditable:YES];
       [self setTarget:self];
       [self setAction:@selector(changeColor:)];
       
   }
   return self;
}

- (IBAction) changeColor: (id) sender
{
   NSColorPanel* colorPanel = [NSColorPanel sharedColorPanel];
   
   // don't send the panel nil
   [colorPanel setColor:
       ( [self objectValue] ? [self objectValue] : [NSColor whiteColor] )];
   
   [colorPanel setTarget:self];
   [colorPanel setAction: @selector(commitColor:)];
   [colorPanel setContinuous:YES];
   
   [colorPanel makeKeyAndOrderFront:self];
}

- (IBAction) commitColor:(id) sender
{
   [self setObjectValue:[sender color]];
}

- (void) drawWithFrame:(NSRect) cellFrame inView:(NSView *) controlView
{
   NSRect borderRect = NSInsetRect (cellFrame, 0.5, 0.5);
   
   // use the smallest size to sqare off the box & center the box
   if (borderRect.size.height < borderRect.size.width) {
       
       borderRect.size.width = borderRect.size.height;
       borderRect.origin.x = borderRect.origin.x + (cellFrame.size.width -
borderRect.size.width) / 2.0;
   
   } else {
       
       borderRect.size.height = borderRect.size.width;
       borderRect.origin.y = borderRect.origin.y + (cellFrame.size.height -
borderRect.size.height) / 2.0;
       
   }
   
   [[NSColor grayColor] set];
   [NSBezierPath strokeRect: borderRect];

   [(NSColor *)[self objectValue] drawSwatchInRect:NSInsetRect
(borderRect, 2.0, 2.0)];
}

Related mailsAuthorDate
mlColorCell, NSActionCell subclass Carl Gieringer Apr 18, 07:53
mlRe: ColorCell, NSActionCell subclass Carl Gieringer Apr 18, 20:59
mlRe: ColorCell, NSActionCell subclass Carl Gieringer Apr 19, 23:33