Subclassing NSTextFieldCell - can't even get started!

  • Well, this has been a thoroughly unpleasant afternoon.

    I'm attempting to modify the "edit" behaviour of an app containing an
    NSOutlineView. A little research reveals that what I am attempting to
    do is change the behaviour of an NSTextFieldCell by subclassing it.
    I've written a really simple subclass, introduced it to IB2 and set
    the class of the appropriate cell to be the name of the subclass but
    can't even get init called, let alone editWithFrame.

    Does anyone have any insights into a step I have missed or otherwise?
    I've been working on a slightly hacked up version of the OutlineView
    sample code and that can be downloaded here (http://www.mediafire.com/?8dmnqlx9zme
    ). If someone could look at it I'd be much obliged.

    Cheers,
    Dave
  • On 2007 Nov, 25, at 19:52, David Preece wrote:

    > ...I am attempting to do is change the behaviour of an
    > NSTextFieldCell by subclassing it. I've written a really simple
    > subclass, introduced it to IB2 and set the class of the appropriate
    > cell to be the name of the subclass

    I never knew you could do that.

    What I do, which works, is to override NSTableColumn, introduce that
    into IB, set the class of my table column(s) to it, and then in the
    implementation of my NSTableColumn subclass, override dataCellForRow:
    to return a cell of my creation.  Example:

    @implementation MyTableColumn

    - (id)dataCellForRow:(int)iRow {
        NSString* identifier = [self identifier] ;
        id item = [[self tableView] itemAtRow:iRow] ;

        // Have a blast making different cells for
        // different rows, columns, item types...
        //

        // simple case
        NSTextFieldCell * cell =[[NSTextFieldCell alloc] init];
        [cell setBordered:YES] ;
        [cell setWhatever:whatever] ;
        return [cell autorelease] ;
    }
  • On Nov 25, 2007, at 7:52 PM, David Preece wrote:

    > I've written a really simple subclass, introduced it to IB2 and set
    > the class of the appropriate cell to be the name of the subclass but
    > can't even get init called, let alone editWithFrame.

    You should probably implement "initWithCoder:" for classes
    instantiated from nib files.
    Also note that cell subclasses typically have to conform to NSCopying.

    j o a r
  • On Mon, 26 Nov 2007 16:52:14 +1300, David Preece <davep...>
    said:
    > Well, this has been a thoroughly unpleasant afternoon.
    >
    > I'm attempting to modify the "edit" behaviour of an app containing an
    > NSOutlineView. A little research reveals that what I am attempting to
    > do is change the behaviour of an NSTextFieldCell by subclassing it.
    > I've written a really simple subclass, introduced it to IB2 and set
    > the class of the appropriate cell to be the name of the subclass but
    > can't even get init called, let alone editWithFrame.
    >
    > Does anyone have any insights into a step I have missed or otherwise?
    > I've been working on a slightly hacked up version of the OutlineView
    > sample code and that can be downloaded here
    (http://www.mediafire.com/?8dmnqlx9zme
    > ). If someone could look at it I'd be much obliged.

    There is an example on your hard drive called DragNDropOutlineView. In that
    example, NSTextFieldCell is subclassed and the subclass is displayed in an
    NSOutlineView. editWithFrame is overridden and the override is called.
    Perhaps looking at that example and comparing it with what you're doing
    could be a useful heuristic. m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
  • Thanks all for suggestions. Mostly by virtue of taking
    DragNDropOutlineView and covering it in breakpoints I can see that the
    method I needed to override was selectWithFrame not editWithFrame. As
    soon as I did that (turning NSInteger into 'int' for those of us
    building against 10.4u), it did as was asked.

    Thanks,
    Dave
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today