which method is called when selecting a cell in the NSTableView in Cocoa OS X?
Asked Answered
H

6

11

I have a NSTableView , i want to get the value present in the cell. I am having only one column so , i just need the row number

i can use this [tableView selectedRow]- but where do i put this i want to put this in a method that gets called on selection of any of the rows.

-(void)tableViewSelectionDidChange:(NSNotification *)notification{

NSLog(@"%d",[tableViewController selectedRow]);

}

The above method also does not work i am getting the error -[NSScrollView selectedRow]: unrecognized selector sent to instance 0x100438ef0]

i want something like the method available in the iPhone tableview-

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
}
Herrin answered 29/5, 2012 at 9:49 Comment(0)
A
31

What is tableViewController object? Only NSTableView instances respond to selectedRow. You can get current table view (the one that sent the notification) from notification's object property:

Objective-C:

-(void)tableViewSelectionDidChange:(NSNotification *)notification{
    NSLog(@"%d",[[notification object] selectedRow]);
}

Swift:

func tableViewSelectionDidChange(notification: NSNotification) {
    let table = notification.object as! NSTableView
    print(table.selectedRow);
}
Appease answered 29/5, 2012 at 9:57 Comment(2)
Is there a way to get the selected cell?Pawpaw
yes, tableView.selectedRow ...have a look here raywenderlich.com/118835/os-x-nstableview-tutorial it'll either return -1 if nothing is selected or 0,1,2,3,4 or whichever row is selected.Moravia
S
3

my 2 cents for Xcode 10/swift 4.2

  func tableViewSelectionDidChange(_ notification: Notification) {
        guard let table = notification.object as? NSTableView else {
            return
        }
        let row = table.selectedRow
        print(row)
    }
Spectacled answered 9/2, 2019 at 12:13 Comment(0)
A
2

In Swift 5.4:

Just use tableView.action = #selector(YOUR_METHOD), then in your method, try to do something with tableView.selectedRow.

This would simply do the trick. Take a look at the demo below:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

    tableView.action = #selector(didSelectRow)
}

@objc private func didSelectRow() {
    print("Selected row at \(tableView.selectedRow)")
}

But please be aware that if nothing is selected, or you clicked outside of a cell, then you'll get a tableView.selectedRow of -1. So you might wanna check if the index is out of range before you use it. :)

Anatomist answered 20/12, 2021 at 6:16 Comment(1)
Works great, a lot easier than I thought it would be.Permeability
P
1

Swift 3 (from Eimantas' answer):

func tableViewSelectionDidChange(_ notification: NSNotification) {
    let table = notification.object as! NSTableView
    print(table.selectedRow);
}
Patio answered 7/5, 2017 at 21:6 Comment(0)
C
0

You should addObserver Notification like this

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(tableViewSelectionDidChangeNotification)
                                             name:NSTableViewSelectionDidChangeNotification object:nil];

It will action when tableView selected row

good luck

Channelize answered 18/5, 2018 at 3:25 Comment(1)
a bit overwhelming.. add and remove observers. we do have a delegate method, yet.Spectacled
T
0

full guide In Swift 5:

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didSelectRow(_:)), name: NSTableView.selectionDidChangeNotification, object: tableView)
    }



    @objc
    func didSelectRow(_ noti: Notification){
        guard let table = noti.object as? NSTableView else {
            return
        }
        let row = table.selectedRow
        print(row)
    }


    deinit {
        NotificationCenter.default.removeObserver(self)
    }
Thousand answered 2/3, 2020 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.