Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click an NSTableView row in Cocoa?

I need my application to open a window when a user double clicks on a row in an NSTableView. I'm having a bit of a difficult time finding information or examples on how to accomplish this. Can anybody point me in the right direction?

like image 292
Allyn Avatar asked Mar 06 '09 22:03

Allyn


4 Answers

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.

like image 199
Jim Puls Avatar answered Nov 19 '22 12:11

Jim Puls


Adding more basic information to @JimPuls answer for the benefit of other newcomers to Cocoa.

  1. 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.
  2. 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.
  3. 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.

like image 39
amateur barista Avatar answered Nov 19 '22 12:11

amateur barista


If someone looks for a swift 2.0 version: This is what works for me. Seems much easier than the Objective C code.

@IBOutlet weak var searchResultTable: NSTableView!

override func viewDidLoad() {
    super.viewDidLoad()
    searchResultTable.doubleAction = "doubleClickOnResultRow"
}

func doubleClickOnResultRow()
{
    print("doubleClickOnResultRow \(searchResultTable.clickedRow)")
}
like image 12
Alfred Schilken Avatar answered Nov 19 '22 10:11

Alfred Schilken


As PR Singh said, you can use cocoa bindings, you can also pass along the selectedObjects.

  1. Select your Table View in IB then in Bindings inspector set these two bindings up as follows:

    >Double Click Target
    
    bind to = Application delegate object (or file owner)
    model key path = self
    selector name = myMethod:
    
    >Double Click Argument
    
    bind to = array controller
    controller key = selectedObjects
    selector name = myMethod:
    

Where myMethod is implemented as

- (void)myMethod:(NSArray*)selectedObjects
{
    NSLog(@"%@", selectedObjects);
}

This is also documented here: https://developer.apple.com/library/mac/qa/qa1472/_index.html

like image 10
GeoffCoope Avatar answered Nov 19 '22 12:11

GeoffCoope