需求:ide
在NSTableView里右键点击一下item刚焦点须要转移到此条目上,但默认行为不是这样的,并且在delegate里也没法实现,只能经过重写方法来实现,并且还须要调用已经废弃的方法。不过这个方法仍然有效。spa
子类实现:ip
- -(NSMenu*)menuForEvent:(NSEvent*)event
- {
- //Find which row is under the cursor
- [[self window] makeFirstResponder:self];
- NSPoint menuPoint = [self convertPoint:[event locationInWindow] fromView:nil];
- int row = [self rowAtPoint:menuPoint];
- /* Update the table selection before showing menu
- Preserves the selection if the row under the mouse is selected (to allow for
- multiple items to be selected), otherwise selects the row under the mouse */
- // BOOL currentRowIsSelected = [[self selectedRowIndexes] containsIndex:row];
- // if no row selected , the variable "row" will be -1
- if (row >= 0)
- {
- [self selectRow:row byExtendingSelection:NO];
- }
- // if no file selected , set the folder menu to the nstableview
- if (row < 0 )
- {
- return self.folderMenu;
- }
- else
- return self.tableItemMenu;
- }
这样点击空白处和点击条目会显示不一样的菜单it
并且焦点也处理的得当。io
可使用selectedRow来获取当前条目的index.event