Re: NSTableView not setting -clickedRow, -clickedColumn as it should
Re: NSTableView not setting -clickedRow, -clickedColumn as it should
- Subject: Re: NSTableView not setting -clickedRow, -clickedColumn as it should
- From: Keary Suska <email@hidden>
- Date: Wed, 08 Aug 2012 07:15:26 -0600
On Aug 7, 2012, at 11:55 PM, Graham Cox wrote:
> According to the release notes for 10.7, NSTableView should now support contextual menus at the individual cell level:
>
>> NSTableView/NSOutlineView - Contextual menu support
>>
>> NSTableView and NSOutlineView now have better contextual menu support. Please see the DragNDropOutlineView demo application for an example of how to properly do contextual menus with a TableView.
>>
>> The key thing to note is that clickedRow and clickedColumn will now both be valid when a contextual menu is popped up. In addition, one can dynamically set the popup menu for a particular cell/column in the delegate method willDisplayCell:. NSTableView handles this by properly overriding menuForEvent:, setting the clickedRow/clickedColumn, and calling [NSCell menuForEvent:inRect:ofView] to find the correct menu. If no menu was returned, the menu for the NSTableView will be used. If one right clicks on a selection of items, all the items are highlighted. One should check to see if clickedRow is one of the selected rows, and if it is then do the menu operation on all the selected rows. The clickedRow and clickedColumn properties will still be valid when the action is sent from the NSMenuItem.
>>
>
>
> But when I implement the method in my custom cell, though it's being called correctly, -clickedRow and -clickedColumn are returning -1. As a result I can't tell which row I right-clicked, so I can't process the menu further. Has this change been revoked in 10.8 (which I'm using at the moment)?
>
> Code:
>
> - (NSMenu*) menuForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)view
> {
> NSMenu* menu = nil;
>
> if([view respondsToSelector:@selector(delegate)])
> {
> id tvDelegate = [(id)view delegate];
>
> if([view isKindOfClass:[NSTableView class]] && [tvDelegate respondsToSelector:@selector(contextualMenuForTableView:tableColumn:row:)])
> {
> NSTableView* tv = (NSTableView*) view;
> NSInteger row, column;
>
> row = [tv clickedRow];
> column = [tv clickedColumn];
I can't speak to why it isn't working as expected but since the "view" argument is the table view you can use rowsInRect:/rowAtPoint: and columnIndexesInRect:/columnAtPoint: to derive the location.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden