Getting string for NSButton in NSTable

I am struggling with this, I cannot help but feel that I am missing something obvious!

I have an NSTable that contains a table cell view, which in turn contains an NSButton.

The button has an installed outlet, and the output is displayed in the table view.

I use the following code to get the index of the row the button is in when the user clicks on this button:

- (IBAction)approveButtonInTable:(id)sender { NSInteger selected = [self.tweetTableView clickedRow]; NSLog(@"sender sends :%ld", selected); } 

However, this always answered with -1, which is not very useful.

Did I miss something very obvious here?

thanks

Gareth

Change 1

I found out that this works if I use it on the output from tableview using this:

 - (IBAction)tweetTableAction:(id)sender { NSInteger selected = [_tweetTableView clickedRow]; NSLog(@"sender sends :%ld", selected); } 

However, this does not help me, since I still cannot get it to work when I press the button, doh!

+4
source share
3 answers

Since you say you are using NSTableCellView , this tells me that you are using a view-based table. In this case, you want to call rowForView: skipping the button. I believe that this method only works in this case (I have never tried it based on a honeycomb table).

 - (IBAction)approveButtonInTable:(id)sender { NSInteger selected = [self.tweetTableView rowForView:sender]; NSLog(@"sender sends :%ld", selected); } 
+4
source

Pressing a button does not match clicking on a row in a table. It’s best to populate the buttons with tags that relate to their position in the table (possibly starting at 100), and then use:

 - (IBAction)approveButtonInTable:(id)sender { NSInteger selected = [sender tag] - 100; // or whatever value you decide to start tagging NSLog(@"sender sends :%ld", selected); } 
0
source

If someone is looking for the Swift 3 version of the Jablair version.

Just implement the @IBAction method.

 @IBAction func approveButtonInTable(_ sender: Any) { let selectedRow = tweetTableView.row(for: sender as! NSView) print("sender sends \(selectedRow)") } 
0
source

Source: https://habr.com/ru/post/1492741/


All Articles