table view, popup, bindings: more grief...

  • Hello all,

    I've bumped into another problem with a table view containing a popup
    column bound to model: As soon as the popup values differ for
    different model object instances, all hell breaks loose.

    A test model code may look like this:

    @interface Model : NSObject {
        int number; // controls the range and interpretation for tag
        int tag; // for positive numbers, 0-1, for negative numbers,
    0-3, for zero zero only
    }
    @end
    @implementation Model // there's a bit more in real code--see the
    archive for complete source, this is the gist
    -(NSArray*)popupValues {
        if (number==0) return [NSArray arrayWithObject:@"none"]; //
    nothing for zero
        if (number>0) return [NSArray arrayWithObjects:@"+",@"+
    +",nil]; // 0-1 all right for positive
        return [NSArray
    arrayWithObjects:@"-",@"--",@"---",@"----",nil]; // 0-3 all right for
    negative
    }
    @end

    There is an NSArrayController which keeps an array of these Models,
    and a table with two columns. First is a plain editor bound to
    "number"; second is a popup whose "contentValues" are bound to
    "popupValues" and "selectedIndex" bound to "tag".

    One would say it's simple enough to be bound to (sorry for the
    pun ;)) work, but it seems the popup cells do not sync with proper
    model object, getting the "contentValues" instead from seemingly
    random one: at first, the table displays all right, but as soon as it
    contains objects with zero, positive, and negative numbers, and you
    try to actually use the popups, ugly things occur (wrong popups
    shown, values don't change when edited, "3" instead of "----"
    displayed for negative "numbers", etc).

    Can anybody please advice how to solve the problem? TIA!

    A complete project to test it, along with slightly improved model
    implementation (change of number should trigger change of popup, also
    change of number zeroes tag) is at http://www.ocs.cz/apps/
    TablePopupProblem.zip.
    ---
    Ondra ÄŒada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    private        <ondra...>            http://www.ocs.cz/oc
  • Hello all,

    just a few more details:

    - a standalone pop-up bound the very same way to selection works
    well; alas, that's not a solution for my project;
    - the culprit does not seem to be the popup itself, but rather the
    bindings: for some godforsaken reason the popup gets depopulated (by
    removeAllItems) *as soon as it is opened* (not, as one would presume,
    closed!), and the *next* item in the controller is asked for its
    popupValues;
    - all the visible popups actually get repopulated pretty often
    without a real need (e.g., when another app -- like the gdb -- goes
    foreground: that does not quite help debuggin' :));
    - thus, it does not seem the popup "keeps old" list; instead, it
    seems to be too eager to get a new one.

    Sadly, not even with my own subclass of NSPopupCell I've been able to
    find a pattern to implement this seemingly extremely simple
    behaviour :( Am I overlooking something quite obvious here?

    === original message with the problem description ===
    I've bumped into another problem with a table view containing a popup
    column bound to model: As soon as the popup values differ for
    different model object instances, all hell breaks loose.

    A test model code may look like this:

    @interface Model : NSObject {
        int number; // controls the range and interpretation for tag
        int tag; // for positive numbers, 0-1, for negative numbers,
    0-3, for zero zero only
    }
    @end
    @implementation Model // there's a bit more in real code--see the
    archive for complete source, this is the gist
    -(NSArray*)popupValues {
        if (number==0) return [NSArray arrayWithObject:@"none"]; //
    nothing for zero
        if (number>0) return [NSArray arrayWithObjects:@"+",@"+
    +",nil]; // 0-1 all right for positive
        return [NSArray
    arrayWithObjects:@"-",@"--",@"---",@"----",nil]; // 0-3 all right for
    negative
    }
    @end

    There is an NSArrayController which keeps an array of these Models,
    and a table with two columns. First is a plain editor bound to
    "number"; second is a popup whose "contentValues" are bound to
    "popupValues" and "selectedIndex" bound to "tag".

    One would say it's simple enough to be bound to (sorry for the
    pun ;)) work, but it seems the popup cells do not sync with proper
    model object, getting the "contentValues" instead from seemingly
    random one: at first, the table displays all right, but as soon as it
    contains objects with zero, positive, and negative numbers, and you
    try to actually use the popups, ugly things occur (wrong popups
    shown, values don't change when edited, "3" instead of "----"
    displayed for negative "numbers", etc).

    Can anybody please advice how to solve the problem? TIA!

    A complete project to test it, along with slightly improved model
    implementation (change of number should trigger change of popup, also
    change of number zeroes tag) is at http://www.ocs.cz/apps/
    TablePopupProblem.zip.
    ---
    Ondra ÄŒada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    private        <ondra...>            http://www.ocs.cz/oc

    _______________________________________________
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list      (<Cocoa-dev...>)
    Help/Unsubscribe/Update your Subscription:
    http://lists.apple.com/mailman/options/cocoa-dev/<ocs...>

    This email sent to <ocs...>
  • If anybody happens to be interested (seems doubtful, given the number
    of replies), here's a *very ugly*, but (in simple cases and
    seemingly) working workaround hack:

    (i) do not bind the popup column at all;
    (ii) ensure there is a shared access to the appropriate array
    controller;
    (iii) use a NSPopupButtonCell subclass like this:

    @implementation OCSPopupCell
    -(void)ocs_popupItemAction:sender {
        [[sender representedObject] setValue:[NSNumber numberWithInt:
    [sender tag]] forKey:@"tag"];
    }
    -(void)populateMenuProperlyForLine:(int)ll {
        TransactionCtl *tc=[TransactionCtl sharedController]; // the
    (singleton) array controller
        id o=[[tc arrangedObjects] objectAtIndex:ll];
        [self removeAllItems];
        int n=0;
        forall (i,[o valueForKey:@"popupValues"]) {
            [self addItemWithTitle:i];
            id<NSMenuItem> mi=[self lastItem];
            [mi setTarget:self];
            [mi setAction:@selector(ocs_popupItemAction:)];
            [mi setRepresentedObject:o];
            [mi setTag:n++];
        }
        [self selectItemAtIndex:[[o valueForKey:@"tag"] intValue]];
    }
    - (void)drawWithFrame:(NSRect)cf inView:(NSTableView *)tv {
        [self populateMenuProperlyForLine:[tv rowsInRect:cf].location];
        [super drawWithFrame:cf inView:tv];
    }
    - (void)attachPopUpWithFrame:(NSRect)cf inView:(NSTableView *)tv {
        [self populateMenuProperlyForLine:[tv rowsInRect:cf].location];
        [super attachPopUpWithFrame:cf inView:tv];
    }
    @end

    Ouch.

    On 31.3.2006, at 21:11, Ondra Cada wrote:

    > Hello all,
    >
    > just a few more details:
    >
    > - a standalone pop-up bound the very same way to selection works
    > well; alas, that's not a solution for my project;
    > - the culprit does not seem to be the popup itself, but rather the
    > bindings: for some godforsaken reason the popup gets depopulated
    > (by removeAllItems) *as soon as it is opened* (not, as one would
    > presume, closed!), and the *next* item in the controller is asked
    > for its popupValues;
    > - all the visible popups actually get repopulated pretty often
    > without a real need (e.g., when another app -- like the gdb -- goes
    > foreground: that does not quite help debuggin' :));
    > - thus, it does not seem the popup "keeps old" list; instead, it
    > seems to be too eager to get a new one.
    >
    > Sadly, not even with my own subclass of NSPopupCell I've been able
    > to find a pattern to implement this seemingly extremely simple
    > behaviour :( Am I overlooking something quite obvious here?
    >
    > === original message with the problem description ===
    > I've bumped into another problem with a table view containing a
    > popup column bound to model: As soon as the popup values differ for
    > different model object instances, all hell breaks loose.
    >
    > A test model code may look like this:
    >
    > @interface Model : NSObject {
    > int number; // controls the range and interpretation for tag
    > int tag; // for positive numbers, 0-1, for negative numbers,
    > 0-3, for zero zero only
    > }
    > @end
    > @implementation Model // there's a bit more in real code--see the
    > archive for complete source, this is the gist
    > -(NSArray*)popupValues {
    > if (number==0) return [NSArray arrayWithObject:@"none"]; //
    > nothing for zero
    > if (number>0) return [NSArray arrayWithObjects:@"+",@"+
    > +",nil]; // 0-1 all right for positive
    > return [NSArray
    > arrayWithObjects:@"-",@"--",@"---",@"----",nil]; // 0-3 all right
    > for negative
    > }
    > @end
    >
    > There is an NSArrayController which keeps an array of these Models,
    > and a table with two columns. First is a plain editor bound to
    > "number"; second is a popup whose "contentValues" are bound to
    > "popupValues" and "selectedIndex" bound to "tag".
    >
    > One would say it's simple enough to be bound to (sorry for the
    > pun ;)) work, but it seems the popup cells do not sync with proper
    > model object, getting the "contentValues" instead from seemingly
    > random one: at first, the table displays all right, but as soon as
    > it contains objects with zero, positive, and negative numbers, and
    > you try to actually use the popups, ugly things occur (wrong popups
    > shown, values don't change when edited, "3" instead of "----"
    > displayed for negative "numbers", etc).
    >
    > Can anybody please advice how to solve the problem? TIA!
    >
    > A complete project to test it, along with slightly improved model
    > implementation (change of number should trigger change of popup,
    > also change of number zeroes tag) is at http://www.ocs.cz/apps/
    > TablePopupProblem.zip.
    > ---
    > Ondra ÄŒada
    > OCSoftware:    <ocs...>              http://www.ocs.cz
    > private        <ondra...>            http://www.ocs.cz/oc
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<ocs...>
    >
    > This email sent to <ocs...>
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<ocs...>
    >
    > This email sent to <ocs...>
    >

    ---
    Ondra ÄŒada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    private        <ondra...>            http://www.ocs.cz/oc