user11209250
user11209250

Reputation:

deselectRow does not work when returning to main screen

I want to make sure the row I selected has been deselected after I return to the main screen where I initially made the selection. However, my code does not work although they are built sucessfully and my code has no errors. Please help!

My code fragment:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? DetailVC {
            destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]

            //deselection animation after returning to main screen
            myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
        }

    }

Upvotes: 0

Views: 154

Answers (2)

Schaheer Saleem
Schaheer Saleem

Reputation: 529

To deselect row you need to write the code in viewWillAppear:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    //deselection animation after returning to main screen
    if let row = myTableView.indexPathForSelectedRow {
        myTableView.deselectRow(at: row, animated: true)
    }   
}

Upvotes: 1

Bhavesh.iosDev
Bhavesh.iosDev

Reputation: 942

Just refresh your tableView on viewWillAppear

override func viewWillAppear(_ animated: Bool) {
        myTableView.reloadData()
    }

and remove Following line it's no longer needed now.

 myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)

Upvotes: 0

Related Questions