Double-click the NSTableView row in Cocoa?

I need my application to open a window when a user double-clicks a row in an NSTableView . I find it difficult to find information or examples of how to do this. Can someone point me in the right direction?

+69
cocoa nstableview double-click
Mar 06 '09 at 22:05
source share
6 answers

Take a look at the -setDoubleAction: method on an NSTableView; you can set this to a method that will be called just like a regular target action system, but with a double click.

In this action method, -clickedRow is useful.

+129
Mar 06 '09 at 22:18
source share

Adding more info to @JimPuls answer for other new to Cocoa.

  • IBOutlet for NSTableView must be declared in the interface. I suggested that this is preferable to do in the table delegate.
  • IBOutlet to the table must be connected via Interface Builder. To do this, drag Ctrl and drag to IB from the class that declares the output as a table. When you release the mouse, a pop-up should appear with the name of the outlet that you specified in step # 1. Select this.
  • In the @implementation section of the -awakeFromNib method, call -setTarget: and -setDoubleAction: on the IBOutlet declared in step # 1 and connected to step # 2.

Here is an excerpt from my table. I have my delegate, which is also configured as a data source, so you will see the NSTableViewDelegate and NSTabeViewDataSource interfaces associated with it.

// Excerpt for the interface.

 @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 

// Excerpt for implementation.

 @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.

+54
Dec 04 '11 at 10:14
source share

As PR Singh said, you can use cocoa bindings, you can also go through selected objects.

  • Select the table view in IB, then the Bindings inspector set these two bindings 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); } 

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

+10
Mar 25 '14 at 14:17
source share

If someone is looking for a quick version 2.0: This is what works for me. This is much simpler than Objective-C code.

 @IBOutlet weak var searchResultTable: NSTableView! override func viewDidLoad() { super.viewDidLoad() searchResultTable.doubleAction = "doubleClickOnResultRow" } func doubleClickOnResultRow() { print("doubleClickOnResultRow \(searchResultTable.clickedRow)") } 
+7
Sep 22 '15 at 20:28
source share

You can enable the double-click action in Interface Builder. Right-click on your table view (make sure you get a table view, not a scroll or view clip or table column) to get your connections pane. Find the "DoubleAction" item in the "Submitted Actions" section. Connect it to the IBAction of your choice.

+6
Nov 11 '15 at 6:10
source share

You can do the same with links, first declare one mentode in the .h file

 -(IBAction)openWindow:(id)sender 

in the .m file implements the same

 -(IBAction)openWindow:(id)sender { //do something here; } 

got to this peer where your table view is present, select the table view and get the second last tab of the attribute inspector, open the double cicl symbol attribute definition to check the owner of the fox selection file, the model patch should be "itself", the selector name will be "openWindow : ", the same thing happens with the expansion of" Double click target "This will work

+1
Sep 23 '13 at 12:59 on
source share



All Articles