NSPopUpButtonCell always select first item

  • I have made the following delegate method below in my window
    controller, by setting a category in NSTableColumn.
    This works fine, showing a popup button in one of the columns of my
    NSOutlineView. I just chose row 5 for simplicity of testing this out.
    I can click the popup button in row 5, and it shows the two menu items
    "number one" and "number two" as it should.
    But my problem is that even if I choose "number two" menu item, or
    other menu item if I have more, it always snaps back to "number one".

    I am baffled by this, and have not been able to find out why this
    happens.
    I of course wants the menu item I selects to be shown after I have
    selected it.

    Ivan

    - (id)tableColumn:(NSTableColumn *)column inOutlineView:(NSOutlineView
    *)outlineView dataCellForRow:(int)row {
    if (row == 5) {
      NSPopUpButton *mybutton = [popUpButtonDict objectForKey:[NSNumber
    numberWithInt:row]];

      if (!mybutton) {
      mybutton = [opopUpButtonCell copy]; //opopUpButtonCell is an outlet
    to an NSPopUpButtonCell in IB
      [mybutton removeAllItems];
      [mybutton addItemWithTitle:@"number one"];
      [mybutton addItemWithTitle:@"number two"];
      [popUpButtonDict setObject:mybutton forKey:[NSNumber
    numberWithInt:row]];
      [opopUpButtonCell release];
      }
      return mybutton;
    } else
      return otextFieldCell; //otextFieldCell is an outlet to an
    NSTextFieldCell in IB
    }
  • > I am baffled by this, and have not been able to find out why this
    > happens.
    > I of course wants the menu item I selects to be shown after I have
    > selected it.

    Does your outline view delegate implement -
    outlineView:setObjectValue:forTableColumn:byItem:? If not, the outline
    view will reset the cell’s object value at the end of editing to the
    the value specified by the data source.

    > - (id)tableColumn:(NSTableColumn *)column inOutlineView:
    > (NSOutlineView *)outlineView dataCellForRow:(int)row {

    I assume this is something you’re implementing on your own? The
    outline view delegate supports:
    - (NSCell *)outlineView:(NSOutlineView *)outlineView
    dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;

    You can return the default value (what you’re using otextFieldCell for
    now) with -[NSTableColumn dataCellForRow:].

    -Ben
  • On 31. jan.. 2008, at 17:05, Benjamin Stiglitz wrote:

    >> I am baffled by this, and have not been able to find out why this
    >> happens.
    >> I of course wants the menu item I selects to be shown after I have
    >> selected it.
    >
    > Does your outline view delegate implement -
    > outlineView:setObjectValue:forTableColumn:byItem:? If not, the
    > outline view will reset the cell’s object value at the end of
    > editing to the the value specified by the data source.

    Ah, that was a great tip. I will try that.

    >
    >
    >> - (id)tableColumn:(NSTableColumn *)column inOutlineView:
    >> (NSOutlineView *)outlineView dataCellForRow:(int)row {
    >
    > I assume this is something you’re implementing on your own? The
    > outline view delegate supports:

    Yes, I have implemented this delegate method myself.

    >
    > - (NSCell *)outlineView:(NSOutlineView *)outlineView
    > dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;
    >
    > You can return the default value (what you’re using otextFieldCell
    > for now) with -[NSTableColumn dataCellForRow:].
    >
    > -Ben
  • I was not using data source, but was binding the column to an
    NSTreeController.
    I tried your suggestion and implementing the below method. I had to
    activate the data source for the method to be used, but the
    NSPopUpButtonCell still snaps back to the first item in the list.

    Ivan

    On 31. jan.. 2008, at 17:05, Benjamin Stiglitz wrote:

    >> I am baffled by this, and have not been able to find out why this
    >> happens.
    >> I of course wants the menu item I selects to be shown after I have
    >> selected it.
    >
    > Does your outline view delegate implement -
    > outlineView:setObjectValue:forTableColumn:byItem:? If not, the
    > outline view will reset the cell’s object value at the end of
    > editing to the the value specified by the data source.
previous month january 2008 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 31      
Go to today