Professional Web Applications Themes

Putting a popup button in an NSTable View - Mac Programming

Hello. I'd like to create a table with a popup button in one column, and I'm not having much luck at it. What I'm trying to do doesn't seem too strange to me -- it's pretty likely that a record would have a field with a limited number of values -- so must be I'm missing something. I'm working with Project Builder and Interface Builder on 10.2.8, by the way. I'll describe my sample code, which shows the behavior I'm experiencing. With IB, I create a simple two column table view, with the tables identified as "col1" and "col2". There's ...

  1. #1

    Default Putting a popup button in an NSTable View

    Hello.

    I'd like to create a table with a popup button in one column, and I'm
    not having much luck at it. What I'm trying to do doesn't seem too
    strange to me -- it's pretty likely that a record would have a field
    with a limited number of values -- so must be I'm missing something.

    I'm working with Project Builder and Interface Builder on 10.2.8, by
    the way. I'll describe my sample code, which shows the behavior I'm
    experiencing.

    With IB, I create a simple two column table view, with the tables
    identified as "col1" and "col2". There's also an "New" button, to add
    a new row to the table. I subclass NSObject, make the connections, and
    create the .h and .m files. For the test case, everything is in one
    class.

    In the code, I create an NSMenu and an NSPopUpButton that uses it. The
    button has five labels, "Zero", "One", "Two", "Three", and "Four", with
    the words matching their indexes. This leads to two questions:

    1. Can I create the popup button in IB and connect it to a column in
    the NSTableView? The column has a "dataCell" connection, but you seem
    to have an open window to create the popup button in. I don't want the
    "template" to show. Am I missing something?

    2. I'd really like the button to be "small" as can be selected in the
    Options pane in IB. Is there a programmatic way to create one? Just
    changing the font and the frame size at initialization doesn't do it,
    but those are the only differences I've seen between my popup button
    and a small one created by IB.

    I associate an action method with the button, then connect the button's
    cell to the NSTableView as the data cell for the appropriate column.

    Now, the surprises start.

    1. When I look at the button, "One" is alwas checked. I've tried
    selectItemAtIndex:0 and I've tried setting autoenablesItems to YES and
    to NO. Nothing changes the behavior.

    2. The sender to the popupAction method is the NSTableView itself, not
    the NSPopUpButton where the action was set. Why?

    3. Accepting the above, how am I supposed to identify the menu? The
    NSTableView shows no selected cell. For now, I'm tracking the active
    row in tableView:objectForTableColumn:row:, but that seems pretty
    cheesy.

    4. Why won't manual selection work for me? Even after I use
    selectItemAtIndex:-1 (to deselect all items) then selectItemAtIndex:n
    to select item n, "One" is selected the next time around. Why?

    The code is available at
    http://sgketcham.home.comcast.net/tvpopup.sitx.bin if you would like to
    download it.

    Thanks for your help,
    Steve
    Steve Guest

  2. #2

    Default Re: Putting a popup button in an NSTable View

    In article <110120042244007765%nospam.net>,
    Steve Ketcham <nospam.net> wrote:
     

    You seem to have some misconceptions about how NSTableView works, so I'm
    snipping most of your post. The dataCell connection is supposed to
    connect to a cell, not a view. An NSPopUpButton is an NSView. You should
    instead use NSPopUpButtonCell.

    If you're using Interface Builder with Xcode, then it's fairly easy to
    do. In the palette where the table view is, there's a popup button thing
    you can drag into your table's data column header. If you're using an
    older version of IB, you have to create it in code. You can use NSCell's
    setControlSize: method to control the size.

    An NSTableView reuses the cell when it draws. It doesn't keep one copy
    of the cell around for each row, it only has one cell for the entire
    column. That means that when you change the cell around, you have to
    keep this in mind, it will be a global change. If you need to make
    per-row changes to the cell, there is an NSTableView delegate method you
    can implement that lets you change the cell right before it's drawn.

    Likewise, NSTableView works differently for cell values. You don't set
    the cell value directly. Rather, NSTableView sets the cell value right
    before it draws the cell, using the object value that it gets from your
    data source. In the case of a popup menu, your data source should return
    an NSNumber that corresponds to the index of the selected item. Your
    data source's set value method will be called with the same kind of
    thing when the user makes a selection.

    You seem to be trying to do things outside of the whole delegate/data
    source idea, which basically won't work. Everything has to go through
    the data source when you're setting values.
    Michael Guest

  3. #3

    Default Re: Putting a popup button in an NSTable View

    In article <mail-91D519.11115512012004localhost>, Michael Ash
    <com> wrote:
     

    I'm not sure what you mean by this, even if you're probably right about
    my misconceptions about NSTableView. I know that NSPopUpButton is an
    NSView, but the associated NSPopUpButtonCell is available with
    NSControl's -cell method. They're used appropriately in the code, I
    think. Or were you talking about IB? If so, I'm sorry I left off the
    "Cell" from the class name, but even adding it doesn't change the fact
    that I can't find a way to associate an uninstantiated
    NSPopUpButtonCell to the NSTableView at 10.2.8. 

    Good news!
     

    Thanks, I'll look at -setControlSize: 

    Are you thinking of -tableView:willDisplayCell:forTableColumn:row: ?
    I'll look at it 

    Thank you for this key information. I probably should have deduced
    this out, knowing that the set value method is handed an NSNumber,
    which I learned by using print-object in the debugger. (I'd already
    worked out that there was only one data cell, the same way.)

    I think this points to the real issue, and the frustration I feel
    whenever I work with the Cocoa doentation. Is there any place where
    the NSNumber return is doented? If your comments below (about
    working outside the model} are accurate, it's only because I spent
    nearly a week reading everything I could find, and looking at examples,
    and never realized that the return value should be an NSNumber. Why
    does it have to be so difficult? Why can't they just say so?
     

    Thanks again. Now that I see how the pieces fit together, it shouldn't
    be too bad.

    Steve
    Steve Guest

  4. #4

    Default Re: Putting a popup button in an NSTable View

    Steve Ketcham <nospam.net> wrote:
     
    >
    > Thank you for this key information. I probably should have deduced
    > this out, knowing that the set value method is handed an NSNumber,
    > which I learned by using print-object in the debugger. (I'd already
    > worked out that there was only one data cell, the same way.)
    >
    > I think this points to the real issue, and the frustration I feel
    > whenever I work with the Cocoa doentation. Is there any place where
    > the NSNumber return is doented?[/ref]

    I don't really know, but you'll be less frustrated if you make mamasam
    part of your studying routine. It's a good compendium of the collective
    wisdom and experience of the Cocoa users. You'd have found this
    immediately:

    http://cocoa.mamasam.com/COCOADEV/2003/03/2/59780.php

    Your app would be further along by now, and you would not need to vent
    here. m.


    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  5. #5

    Default Re: Putting a popup button in an NSTable View

    In article <120120040823537733%nospam.net>,
    Steve Ketcham <nospam.net> wrote:
     
    >
    > I'm not sure what you mean by this, even if you're probably right about
    > my misconceptions about NSTableView. I know that NSPopUpButton is an
    > NSView, but the associated NSPopUpButtonCell is available with
    > NSControl's -cell method. They're used appropriately in the code, I
    > think. Or were you talking about IB? If so, I'm sorry I left off the
    > "Cell" from the class name, but even adding it doesn't change the fact
    > that I can't find a way to associate an uninstantiated
    > NSPopUpButtonCell to the NSTableView at 10.2.8.[/ref]

    Sorry, I misunderstood what you meant in this part.

    You could create an NSPopUpButton in a separate view from your window,
    and simply never display this view, and then use the cell from the
    button in your table. That would let you configure things in IB instead
    of code if that's what you're after.
     
    >
    > Are you thinking of -tableView:willDisplayCell:forTableColumn:row: ?
    > I'll look at it[/ref]

    Yes, that's right. In that method, you can modify the cell however you
    need before it's drawn for a particular row.
     
    >
    > Thank you for this key information. I probably should have deduced
    > this out, knowing that the set value method is handed an NSNumber,
    > which I learned by using print-object in the debugger. (I'd already
    > worked out that there was only one data cell, the same way.)
    >
    > I think this points to the real issue, and the frustration I feel
    > whenever I work with the Cocoa doentation. Is there any place where
    > the NSNumber return is doented? If your comments below (about
    > working outside the model} are accurate, it's only because I spent
    > nearly a week reading everything I could find, and looking at examples,
    > and never realized that the return value should be an NSNumber. Why
    > does it have to be so difficult? Why can't they just say so?[/ref]

    They can't just say so because this is an object-oriented system.
    NSTableView is not a view which takes a bunch of values and draws them
    on the screen. It's an *interface* between a data source, which provides
    values of some kind, and some cells, which take values of some kind. The
    table view neither knows nor *cares* what kind of values you provide or
    what kind of values the cells take. It just takes the value you give it
    and hands it off to your cell.

    NSTableView *defaults* to NSTextFieldCell, but it's not *required* to
    use it. I think you saw that and got into the idea that any change to
    the cell type was some customization and should be handled specially.
    But it's not. The way you can know that you need to provide an NSNumber
    is because NSPopUpButtonCell takes an NSNumber. In the docs:

    - (void)setObjectValue:(id <NSCopying>)object

    Attempts to select the item at an index of object if the receiver
    responds to intValue and object is a valid index. Otherwise, the
    selected item is cleared.

    From this you can see that an NSString would work too; it responds to
    intValue, so you could return something like "3" to select the third
    item, for example.

    Likewise, when NSTableView gives you an object value, it just gives you
    the result of objectValue from the cell. So again:

    - (id <NSCopying>)objectValue

    Returns the index of the selected item. It is an NSNumber containing
    -[NSPopUpButtonCell indexOfSelectedItem].

    You simply weren't looking in the right place.
    Michael Guest

  6. #6

    Default Re: Putting a popup button in an NSTable View

    In article <1g7fuux.1t3f9o91ic0mfkN%com>, matt neuburg
    <com> wrote:
     

    Thanks for the link. It looks very interesting, and I've registered
    already. I'm looking forward to looking at it in detail.
     

    At least it was a gentle vent, and I stayed on topic :)

    S
    Steve Guest

  7. #7

    Default Re: Putting a popup button in an NSTable View

    Steve Ketcham <nospam.net> wrote:
     
    >
    > At least it was a gentle vent, and I stayed on topic :)[/ref]

    I'm not criticising your speech, I'm trying to help your mood. Checking
    the mamasam archives has helped me avoid frustration many, many times.
    It is a major Cocoa resource. If you use it you'll be happier. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

Similar Threads

  1. Browser Resize/Maximize is not putting the PopUp at theCentre
    By VishalHegde in forum Macromedia Flex General Discussion
    Replies: 3
    Last Post: November 15th, 12:57 PM
  2. Popup window on a button
    By Matty2G in forum Macromedia Flash Actionscript
    Replies: 4
    Last Post: February 14th, 03:56 PM
  3. Popup button problem
    By Loris Cecchinelli in forum Mac Programming
    Replies: 0
    Last Post: September 8th, 12:26 PM
  4. Popup Window on Button Push
    By Temp in forum ASP.NET General
    Replies: 5
    Last Post: August 11th, 03:33 AM
  5. PopUp for Button
    By Saravana in forum ASP.NET General
    Replies: 0
    Last Post: July 21st, 03:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139