简体   繁体   中英

NSTableView : Context menu for editing column configuration

In some OS X apps, control-clicking on the header of an NSTableView brings up a context menu, that lets the user choose which columns are visible.

Is this something I'll need to implement manually, or is this some hard to find Cocoa feature?

AFAIK, it isn't a standard feature of NSTableView. You have to implement it on your own by setting the menu of NSTableHeaderView.

  NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
  menu.font = [NSFont menuFontOfSize:[NSFont smallSystemFontSize]];
  menu.showsStateColumn = YES;
  for (NSTableColumn *column in tableView.tableColumns) {
    NSMenuItem *item = [menu addItemWithTitle:column.headerToolTip action:@selector(toggleTableColumn:) keyEquivalent:@""];
    item.state = [column isHidden] ? NSOffState : NSOnState;
    item.representedObject = column;
  }
  tableView.headerView.menu = menu;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM