NSPopUpButtonCell in NSTableView

Discussion in 'Mac Programming' started by tmeehan, Mar 5, 2009.

  1. tmeehan macrumors newbie

    Feb 25, 2009
    I've got a two column tableview - column 0 is text, column 1 is popupcells. Everything seems fine but when I release the mouse when selecting from the popup, it momentarily displays the selection i made but it then resets back to item 0 (first item) in the menu list. I've gone over this million times. In my datasource class I have the 3 required routines. It all comes down to the fact that I can't get the selected item index - it always returns 0.

    In the routine:
    -(id)tableView: (NSTableView *) tableView objectValueForTableColumn:(NSTableColumn *) tableColumn row: (int) row

    I do the following:
    selection = [[tableColumn dataCell] indexOfSelectedItem];

    this always returns 0. I have verified that I do indeed have the correct datacell by verifying it's itemArray. Everything checks out but the "indexOfSelectedItem" method always returns 0.

    I manually set the returned value to "2" just to see if the pop up would get set to the third item - It does. I just can't get the dataCell to tell me what item is selected.

    And then down in the other routine:

    - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

    if I manually set the value to 2 for example, my popup changes correctly. I am going nuts!

    I am unsure of a couple of things; for a popup control in a listview, am I supposed to use the "setObjectValue" method or should the popupbuttoncell class take care of maintaining the currently displayed item?

    For the text column, I am storing the values in an NSMutableArray. That's all working fine. But when my datasource is asked for the value of the popupmenu, i assume I have to return the current item index. If I manually return a value it responds correctly - but like I have said numerous times - i don't seem to be able to query the datacell for its currently selected index.

    It's almost as if internally a copy of my datacell is being used but it is not a complete copy. It has all the right menuitems in it but for some reason it can't provide the currently selected item index.

    All and all help with this would be greatly appreciated.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    When the popup selection changes, it will call back tableView:setObjectValue:forTableColumn:row: - the objectValue is what you want. This will be an NSNumber containing the index of the selected item. You will then need to update your data to hold this value, so that next time it updates in tableView:eek:bjectValueForTableColumn:row: it will use this value. And yes, in that method return an NSNumber containing the index of the item you want selected. If you want finer control, you can implement the tableView:willDisplayCell:forTableColumn:row: method.
  3. tmeehan thread starter macrumors newbie

    Feb 25, 2009

    Thanks for that reply. That made all the difference.

Share This Page