How to make the menu item "Panel" the displayed number of rows in the table?

I have a basic data table and would like the menu item to display the number of rows in the table. I have already created a menu item using this code:

 -(void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
NSStatusItem *statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; //Create new status item instance
[statusItem setHighlightMode:YES]; //This does something, I'm sure of it.
[statusItem setTitle:[NSString stringWithFormat:@"%C",0xff50]]; //This labels it. You can also use setImage instead to use an icon. That current code will result in a item labeled "p"
[statusItem setEnabled:YES]; //Self explanatory
[statusItem setMenu:theMenu];
[statusItem setToolTip:@"TOOLTIP HA AWESOME AMIRITE?"]; //Optional, just for kicks.
}

What do I need to add so that the menu item displays the number of rows in the table?

+3
source share
1 answer

If you don't need a live update, you can try this approach:

1) set the delegate from the menu:

[theMenu setDelegate:self];

2) and implement the delegate method:

- (void)menuWillOpen:(NSMenu *)menu {
    NSUInteger count = [self.tableView numberOfRows];
    [[menu itemAtIndex:0] setTitle: [NSString stringWithFormat:@"%d rows", count]];
}

, . , - , KVO . KVO, StatusItem.

+4

Source: https://habr.com/ru/post/1707658/


All Articles