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

后端 未结 5 954
灰色年华
灰色年华 2021-01-02 03:59

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 selectedRo

相关标签:
5条回答
  • 2021-01-02 04:31

    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)
        }
    
    0 讨论(0)
  • 2021-01-02 04:38

    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);
    }
    
    0 讨论(0)
  • 2021-01-02 04:46

    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)
        }
    
    0 讨论(0)
  • 2021-01-02 04:46

    Swift 3 (from Eimantas' answer):

    func tableViewSelectionDidChange(_ notification: NSNotification) {
        let table = notification.object as! NSTableView
        print(table.selectedRow);
    }
    
    0 讨论(0)
  • 2021-01-02 04:48

    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

    0 讨论(0)
提交回复
热议问题