I am struggling with this one, can't help but feel i am missing something obvious!
I have an NSTable which contains a Table Cell View, which in turn contains an NSButton.
The button has an outlet defined, and the table view has an outlet defined.
I am using the following code to get the index of the row a button is in when the user clicks that button:
- (IBAction)approveButtonInTable:(id)sender {
NSInteger selected = [self.tweetTableView clickedRow];
NSLog(@"sender sends :%ld", selected);
}
However this always replied with a -1, which is not massively helpful.
Am i missing something very obvious here?
Thanks
Gareth
Edit 1
I have worked out that this does work if i use it on the outlet of the tableview, using this:
- (IBAction)tweetTableAction:(id)sender {
NSInteger selected = [_tweetTableView clickedRow];
NSLog(@"sender sends :%ld", selected);
}
However that doesn't really help me as i still can't get it to work when i click on the button, doh!
Since you say you're using an NSTableCellView
, that tells me you're using a view-based table. In that case, you want to call rowForView:
, passing in the button. I believe this method only works in the case (never tried it on a cell-based table).
- (IBAction)approveButtonInTable:(id)sender {
NSInteger selected = [self.tweetTableView rowForView:sender];
NSLog(@"sender sends :%ld", selected);
}
If someone looking a Swift 3 version of Jablair answer.
Just implement @IBAction method.
@IBAction func approveButtonInTable(_ sender: Any) {
let selectedRow = tweetTableView.row(for: sender as! NSView)
print("sender sends \(selectedRow)")
}
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