Double click an NSTableView row in Cocoa?
Take a look at the -setDoubleAction:
method on NSTableView; you can set that to a method that will be called just like the normal target-action system but on a double-click.
In that action method, -clickedRow
will be useful.
Adding more basic information to @JimPuls answer for the benefit of other newcomers to Cocoa.
- An IBOutlet to the NSTableView needs to be declared in an interface. I assumed it is preferred to do so in the table's delegate.
- The IBOutlet to the table needs to be connected via Interface Builder. To do that Ctrl-Drag & Drop in IB from the class that declares the outlet to the table view. When you release your mouse a popup should appear with the name of the outlet you declared in step #1. Select that.
- In the @implementation section, on the -awakeFromNib method, call -setTarget: and -setDoubleAction: on the IBOutlet declared in step #1 and connected in step #2.
Here's an excerpt from my table view delegate. I have my delegate also set up as the datasource, so that's why you'll see both the NSTableViewDelegate and NSTabeViewDataSource interfaces associated with it.
// Interface excerpt.
@interface MyTableViewDelegate : NSObject <NSTableViewDelegate, NSTableViewDataSource>
{
// This iVar needs to be connected to the table view via the IB.
IBOutlet NSTableView *tableOutlet;
}
@property (assign) IBOutlet NSTableView *tableOutlet;
- (void)doubleClick:(id)nid;
@end
// Implementation excerpt.
@implementation MyTableViewDelegate
@synthesize tableOutlet = _tableOutlet;
- (void)awakeFromNib {
[_tableOutlet setTarget:self];
[_tableOutlet setDoubleAction:@selector(doubleClick:)];
}
- (void)doubleClick:(id)object {
// This gets called after following steps 1-3.
NSInteger rowNumber = [_tableOutlet clickedRow];
// Do something...
}
Hope this helps.