PopUp menus in tables
PopUp menus in tables
- Subject: PopUp menus in tables
- From: "Frank D. Engel, Jr." <email@hidden>
- Date: Mon, 03 Oct 2016 19:29:34 -0400
Hi,
I have an NSPopupButtonCell in a cell-based table (MacOS X). The table
is being fed by an NSArrayController, but the set of items in this
particular popup menu is fixed (entered them as NSMenuItem objects
within Xcode's Interface Builder).
However, I need to enable/disable certain items based on various
conditions for the particular row of the table view that I am popping up
the menu for.
I tried binding the "enabled" property of the menu items to a property
of the "selection" of the array controller. At first I thought this
wasn't working quite right, or I had an off-by-one error somewhere, but
after playing with it for a while, I realized that it was setting the
enabled property based on the row that was selected before I tried to
open the menu, rather than the row of the table that the menu was on.
In other words, if the item should only be disabled for the last row of
the table, and I open the menu on that row even though the first row had
been highlighted before the click that opened the menu, it was enabled,
even though the last row appeared to be selected. However, the next time
I opened the menu, regardless of which row of the table view it was on,
unless I selected an intermediate row first, the item was disabled -
because the last row of the table had been selected.
To try to work around this, I removed the bindings and set one of my
classes as the delegate of the NSMenu object that was associated with
the popup cell, added IBOutlets to that class and pointed them to the
table and to two menu items I wanted to set based on this condition, and
added the following code to that class (I tweaked some names to make the
code more clear out of context):
- (void)doMenuUpdate:(NSMenu *)menu
{
NSLog(@"here");
DataElement *el = [[tableItemsArrayController arrangedObjects]
objectAtIndex:[theTable selectedRow]];
[item1 setEnabled:[el shouldEnableItem]];
[item2 setEnabled:[el shouldEnableItem]];
}
- (void)menuNeedsUpdate:(NSMenu *)menu
{
dispatch_async(dispatch_get_main_queue(), ^{
[self doMenuUpdate:menu];
});
}
With that code in place, I did get the "here" being displayed right
after the menu was opened (not waiting for it to close), but the
behavior was exactly the same as with the bindings.
It appears that the tableview is only updating its "selectedRow"
property AFTER the menu closes, instead of when it opens.
This is not very helpful in this scenario.
Does anyone know of a way to determine which row of the table the menu
is being opened for, rather than the one that was selected before I
tried to open it?
I'm trying to avoid going to a view-based table as I find them
relatively frustrating to work with and don't like the way they wind up
looking quite as much. I'll use them if I must, but I prefer not to.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden