• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Subclassing NSSearchFieldCell
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclassing NSSearchFieldCell


  • Subject: Re: Subclassing NSSearchFieldCell
  • From: Stefan Fisk <email@hidden>
  • Date: Thu, 3 Nov 2005 01:44:01 +0100

I just came up with a most evil solution! =)

If you override the control's -initWithDecoder: in the following manner you can convert the cell to your custom subclass:

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
/* if the cell is not an instance of the control's cell class, but a super class of it */
if ([[self cell] class] != [[self class] cellClass]
&&
[[[self class] cellClass] isSubclassOfClass:[[self cell] class]])
{
NSCell oldCellCopy = [self->_cell copy];


NSCell newCell = [ConvertObjectIntoSubclass(oldCellCopy, [AnimatedSearchFieldCell class]) autorelease];
[newCell awakeAfterUsingCoder:aDecoder]
[self setCell:newCell];


            NSDeallocateObject(oldCellCopy);
        }
    }

    return self;
}

#import "/usr/include/objc/objc-class.h"

/*
* Creates a copy of object that is an instance of subclass.
* The copy will have a retain count of one.
* The instance variables that subclass adds will be filled with garbage.
*/
id ConvertObjectIntoSubclass(id object, Class subclass) {
NSCParameterAssert([subclass isSubclassOfClass:[object class]]);


/* Creates a copy of object with the subclass's instance size */
id copy = NSCopyObject(object, subclass->instance_size - [object class]->instance_size, NSZoneFromPointer(object));


    NSCAssert(copy, @"Failed to copy object.");

	/* Turns the object into an instance of subclass */
    copy->isa = subclass;

    return copy;
}

This requires that the cell subclass does it's initialization in - awakeAfterUsingCoder:, which isn't exactly how things should be. Still, it's not, from the cell's point of view at least, to odd, and should also work when decoding instances the normal way. Most certainly this will break under some circumstances, so ymmv.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Subclassing NSSearchFieldCell (From: Hamish Allan <email@hidden>)
 >Re: Subclassing NSSearchFieldCell (From: Stefan Fisk <email@hidden>)
 >Re: Subclassing NSSearchFieldCell (From: Hamish Allan <email@hidden>)
 >Re: Subclassing NSSearchFieldCell (From: Stefan Fisk <email@hidden>)
 >Re: Subclassing NSSearchFieldCell (From: Hamish Allan <email@hidden>)

  • Prev by Date: Re: Crash in _gatherFocusStateInto:upTo:withContext:?
  • Next by Date: Flushing events
  • Previous by thread: Re: Subclassing NSSearchFieldCell
  • Next by thread: NSOutlineView: expand all items. How?
  • Index(es):
    • Date
    • Thread