Re: Binding to custom NSCell
Re: Binding to custom NSCell
- Subject: Re: Binding to custom NSCell
- From: Ken Tozier <email@hidden>
- Date: Thu, 12 Feb 2009 16:18:24 -0500
24 hours later, still completely stumped on this one. The sticking
point seems to be that Apple's NSTextFieldCell is doing something that
is not clearly defined in the Key-Value Observing/Key-Value coding
documentation to allow the following to work
[column bind: @"value" toObject: arrayController withKeyPath:
@"arrangedObjects.name" options: nil];
I suspect that all I'm missing is some required accessor, but nowhere
in the documentation does Apple offer a step by step example on how to
write a bindable custom NSCell subclass. What methods are absolutely
required? What methods are optional?
Here's my custom cell code. Where am I going wrong?
Thanks for any help
@interface PMXDocumentCell : NSCell
{
NSButtonCell *addButton;
NSImage *backImage,
*backSelectedImage,
*addButtonImage,
*addButtonSelectedImage,
*currentBackImage;
NSString *documentName;
NSDictionary *textAttributes;
}
+ (id) documentCell;
- (void) initImages;
- (void) initSubCells;
- (NSString *) documentName;
- (void) setDocumentName:(NSString *) inName;
- (id) objectValue;
- (void) setObjectValue:(id) inObject;
@end
#define ADD_BUTTON_SIZE 17
@implementation PMXDocumentCell
+ (id) documentCell
{
return [[[PMXDocumentCell alloc]
init]
autorelease];
}
- (id) init
{
self = [super init];
if (self)
{
[self initImages];
[self initSubCells];
currentBackImage = backImage;
textAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSColor blackColor], NSForegroundColorAttributeName,
[NSFont systemFontOfSize:13], NSFontAttributeName,
nil];
}
return self;
}
- (void) initImages
{
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
NSString *path;
path = [bundle pathForResource: @"add_button" ofType: @"png"];
addButtonImage = [[NSImage alloc] initWithContentsOfFile: path];
path = [bundle pathForResource: @"add_button_white" ofType:
@"png"];
addButtonSelectedImage = [[NSImage alloc] initWithContentsOfFile:
path];
path = [bundle pathForResource: @"toolbar_25" ofType: @"png"];
backImage = [[NSImage alloc] initWithContentsOfFile: path];
path = [bundle pathForResource: @"toolbar_25_focus" ofType:
@"png"];
backSelectedImage = [[NSImage alloc] initWithContentsOfFile: path];
// set resizing
[addButtonImage setScalesWhenResized: YES];
[addButtonSelectedImage setScalesWhenResized: YES];
[backImage setScalesWhenResized: YES];
[backSelectedImage setScalesWhenResized: YES];
// size button images
[addButtonImage setSize: NSMakeSize(ADD_BUTTON_SIZE, ADD_BUTTON_SIZE)];
[addButtonSelectedImage setSize: NSMakeSize(ADD_BUTTON_SIZE,
ADD_BUTTON_SIZE)];
}
- (void) initSubCells
{
addButton = [[NSButtonCell alloc] init];
[addButton setBordered: NO];
[addButton setButtonType: NSMomentaryChangeButton];
[addButton setImage: addButtonImage];
[addButton setAlternateImage: addButtonSelectedImage];
}
- (void) drawWithFrame:(NSRect) inCellFrame
inView:(NSView *) inControlView
{
NSRect buttonFrame = NSMakeRect(inCellFrame.size.width -
ADD_BUTTON_SIZE - 6, inCellFrame.origin.y, ADD_BUTTON_SIZE,
ADD_BUTTON_SIZE),
nameFrame = NSMakeRect(6, inCellFrame.origin.y,
inCellFrame.size.width - ADD_BUTTON_SIZE - 12 , ADD_BUTTON_SIZE);
NSPoint textPoint = NSMakePoint(inCellFrame.origin.x + 1,
inCellFrame.origin.y);
currentBackImage = [self isHighlighted] ? backSelectedImage :
backImage ;
[currentBackImage drawAtPoint: inCellFrame.origin
fromRect: NSZeroRect
operation: NSCompositeSourceOver
fraction: 1.0];
[addButton drawWithFrame: buttonFrame
inView: inControlView];
[documentName drawInRect: nameFrame
withAttributes: textAttributes];
}
- (NSString *) objectValue
{
return documentName;
}
- (void) setObjectValue:(id) inObject
{
if (inObject == nil)
inObject = @"document name";
if ([inObject isKindOfClass: [NSString class]])
[self setStringValue: inObject];
else
[NSException raise: NSInvalidArgumentException format: @"%@ Invalid
object %@", NSStringFromSelector(_cmd), inObject];
}
- (NSString *) documentName
{
return documentName;
}
- (void) setDocumentName:(NSString *) inName
{
[inName retain];
[documentName release];
documentName = inName;
// Tell control view to redisplay us.
[(NSControl *)[self controlView] updateCell: self];
}
@end
The table column data cell is set to an instance of my
"PMXDocumentNameCell " and bound using the exact same syntax that
works for NSTextFieldCell
[column bind: @"value" toObject: arrayController withKeyPath:
@"arrangedObjects.name" options: nil];
_______________________________________________
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