需求: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