tableViewSelectionDidChange does not work in Xcode 8

Asked

Viewed 30 times

1

After upgrading to macOS Sierra, and consequently to Xcode 8, the function tableViewSelectionDidChange no longer works. I think my code is correct.

func tableViewSelectionDidChange(_ notification: Notification) {

        if let selectedRow = (notification.object as AnyObject).selectedRow {
            if ( selectedRow > -1 ) {
                selectedItem = data[selectedRow]
                if let nome = selectedItem?.value(forKey: "nome") as? String {
                    nomeField.stringValue = nome
                } else {
                    blankOutField(nomeField)
                }


    override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = NSApplication.shared().delegate as! AppDelegate
        managedContext = appDelegate.managedObjectContext
        tableView.delegate = self
        tableView.dataSource = self
        fetchDataAndRefreshTable()
    }

    func fetchDataAndRefreshTable() {
        let fetchRequest = NSFetchRequest<NSFetchRequestResult> (entityName: "CadastroPacientes")
        let sortDescriptor = NSSortDescriptor(key: "nome", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))
        fetchRequest.sortDescriptors = [sortDescriptor]
        do {
            data = try managedContext!.fetch(fetchRequest) as! [NSManagedObject]
        } catch {
            Swift.print(error)
        }
    }

I cannot find the solution because it seems that the line is not selected, nothing happens when I click on the line in TableView.

the Program is using Core Data, before everything worked perfectly.

  • Problem solved. It was enough to change the binary operator to ( selectedItem >= -1 ) and everything worked correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.