Skip to content

Instantly share code, notes, and snippets.

@CyrilPeponnet
Created April 8, 2013 15:58
Show Gist options
  • Save CyrilPeponnet/5337935 to your computer and use it in GitHub Desktop.
Save CyrilPeponnet/5337935 to your computer and use it in GitHub Desktop.
CPMenuItem issue when using removeAllItems when items are instantiated in the parent class.
/*
* AppController.j
* CPTableViewGroupRows
*/
@import <Foundation/CPObject.j>
@import <AppKit/CPMenu.j>
@import <AppKit/CPMenuItem.j>
@implementation AppController : CPObject
{
CPMenu _myMenu;
CPMenuItem _anInstanceItem;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
scrollView = [[CPScrollView alloc] initWithFrame:[contentView bounds]];
tableView = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
[tableView setUsesAlternatingRowBackgroundColors:YES];
data = [[CPArray alloc] init];
[data addObject:@"test"];
[data addObject:@"another 1"];
var theColumn = [[CPTableColumn alloc] initWithIdentifier:@"theColumn"];
[[theColumn headerView] setStringValue:@"The Column"];
[tableView addTableColumn:theColumn];
[theColumn setMinWidth:300]
[scrollView setDocumentView:tableView];
[tableView setDataSource:self];
[tableView setDelegate:self];
[contentView addSubview:scrollView];
[theWindow orderFront:self];
_myMenu = [[CPMenu alloc] init];
_anInstanceItem = [[CPMenuItem alloc] initWithTitle:"Instance item : This one will remain among futur right clicks" action:@selector(action:) keyEquivalent:nil];
}
- (int)numberOfRowsInTableView:(CPTableView)aTableView
{
return [data count];
}
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aTableColumn row:(int)rowIndex
{
return data[rowIndex];
}
- (BOOL)tableView:(CPTableView)aTableView isGroupRow:(int)rowIndex
{
return rowIndex == 5;
}
-(void)action:(id)aSender
{
var alert = [[CPAlert alloc] init];;
[alert setTitle:"aTitle"];
[alert setMessageText:"aText"];
[alert runModal];
}
- (CPMenu)tableView:(CPTableView)aTableView menuForTableColumn:(CPTableColumn)aColumn row:(int)aRow;
{
[_myMenu removeAllItems]; /* this is used to dynamicly build the CPMenu against the selected row
but this is the source of the issue for instanciated items */
[[_myMenu addItemWithTitle:"Not an Instance Item : Hightligh will not persist" action:@selector(action:) keyEquivalent:nil] setTarget:self];
[[_myMenu addItem:_anInstanceItem] setTarget:self];
return _myMenu;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment