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

跟風遠走 提交于 2019-12-01 01:29:55

问题


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  {
}

回答1:


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);
}



回答2:


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)
    }



回答3:


Swift 3 (from Eimantas' answer):

func tableViewSelectionDidChange(_ notification: NSNotification) {
    let table = notification.object as! NSTableView
    print(table.selectedRow);
}



回答4:


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



来源:https://stackoverflow.com/questions/10796724/which-method-is-called-when-selecting-a-cell-in-the-nstableview-in-cocoa-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!