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?
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.
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.
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)")
}
As PR Singh said, you can use cocoa bindings, you can also pass along the selectedObjects.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With