So, I really think there must be a 'proper' way to get the editor to pick up and return the value (name) I need editing from my cell's model object, but I'm still searching for the white magic, having dabbled in the darker sort.


OK, I think I finally (mostly) grokked it.

The NSCell subclass can pretend it was given a string object (the name of the actual bound object) and palm this off on its superclass (NSTextFieldCell). The actual object given can be stashed in 'representedObject', so long as you remember to copy this in copyWithZone: and this can be used to drive all the extras in any custom drawing code for the cell.

You can pick up the string at the end of editing, with the cell's "endEditing:"

So I have code like this:

- (id)copyWithZone:(NSZone *)zone {
    NSCell *newCell = [super copyWithZone:zone];
    [newCell setRepresentedObject:[self representedObject]];
    return newCell;
}

- (void)setObjectValue:(id)object {
    if ([object isKindOfClass:[Thing class]]) {
        [self setRepresentedObject:object];
        [super setObjectValue:[object name]];
    }
}

- (void)endEditing:(NSText *)textObj {
    [super endEditing:textObj];

    // Set the representedObject's text
[[self representedObject] setValue:[textObj string] forKey:@"name"];
}

Thankfully there's no messing with (abusing?) NSFormatter subclasses this way,

The only remaining fly in the ointment is that the model object ("Thing" class here, the object stored in representedObject) still gets a KVC message to set the OLD value of cell to the "" (empty string) key, which I currently just defend against, ignoring the value. I'd love to know how to prevent this from happening.

-- lwe

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to