- (void)rightMouseDown:(NSEvent *)event
NSViewはこれを次のビューに渡しません。このメソッドは、現在のクラスにmenuForEvent:があることを確認し、ある場合は呼び出されます。そうでない場合、それは終了し、他に何も起こりません。これが、テーブルビューがrightMouseDown:を飲み込むため、NSTableCellViewがmenuForEvent:に応答しない理由です。
テーブルビューをサブクラス化し、rightMouseDown:イベントを処理して、NSTableCellViewのrightMouseDown:を呼び出し、ストーリーボードで作成してNSTableViewCellに接続したメニューの表示を処理できます。
サブクラス化されたNSTableViewでの私のソリューションは次のとおりです。
- (void)rightMouseDown:(NSEvent *)event
{
for (NSTableRowView *rowView in self.subviews) {
for (NSView *tableCellView in [rowView subviews]) {
if (tableCellView) {
NSPoint eventPoint = [event locationInWindow];
// NSLog(@"Window Point: %@", NSStringFromPoint(eventPoint));
eventPoint = [self convertPoint:eventPoint toView:nil];
eventPoint = [self convertPoint:eventPoint toView:self];
// NSLog(@"Table View Point: %@", NSStringFromPoint(eventPoint));
NSRect newRect = [tableCellView convertRect:[tableCellView bounds] toView:self];
// NSLog(@"Rect: %@", NSStringFromRect(newRect));
BOOL rightMouseDownInTableCellView = [tableCellView mouse:eventPoint inRect:newRect];
// NSLog(@"Mouse in view: %hhd", mouseInView);
if (rightMouseDownInTableCellView) {
if (tableCellView) {
// Lets be safe and make sure that the object is going to respond.
if ([tableCellView respondsToSelector:@selector(rightMouseDown:)]) {
[tableCellView rightMouseDown:event];
}
}
}
}
}
}
}
これにより、右マウスイベントが発生した場所が検出され、正しいビューがあるかどうかが確認され、rightMouseDown:がそのビューに渡されます。
この解決策があなたのために働くかどうか私に知らせてください。