Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting row for NSButton in NSTable

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!

like image 661
Gareth Jeanne Avatar asked Mar 24 '23 02:03

Gareth Jeanne


2 Answers

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);
}
like image 52
Jablair Avatar answered Apr 02 '23 06:04

Jablair


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)")
 }
like image 35
hamsternik Avatar answered Apr 02 '23 06:04

hamsternik