viewWillAppear not calling didDeselectRowAt

I currently have a tableView which I've created custom selection and deselection actions for (fade in and out a view). I am facing a problem where on unwind back to the tableView the deselect action isn't being called. I have added the necessary deselect code to my viewWillAppear so can't seem to work out what could be going wrong. Is there a different method for this use-case?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Deselect row on unwind
    if let path = folderTableView.indexPathForSelectedRow {
        folderTableView.deselectRow(at: path, animated: true)
    }
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {
    print("Select")

    switch indexPath.section {
    case 2:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
        cell.folderTagSelectionBKG.alpha = 1.0
    default:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
        cell.folderSelectionBKG.alpha = 1.0
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)  {
    print("Should deselect")

    switch indexPath.section {
    case 2:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderTagSelectionBKG.alpha = 0
        })
    default:
        let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
            cell.folderSelectionBKG.alpha = 0
        })
    }
}

4 answers

  • answered 2019-11-14 05:18 Manikandan

    If you using UITableViewController subclass then just set property

    
    self.clearsSelectionOnViewWillAppear = YES;
    

    else on viewDidAppear just call

    NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
    if (indexPath) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
    }
    
    // MARK: - Swift 3
    if let indexPath = tableView.indexPathForSelectedRow {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    

    How to unselect a uitableview cell when the user returns to the view controller

  • answered 2019-11-14 08:32 Aira Samson

    From your tableView unwind action, try to put your deselect code. Like this:

    @IBAction func <UnwindName>(segue : UIStoryboardSegue) {
    
        if let indexPath = tableView.indexPathForSelectedRow {
           tableView.deselectRow(at: indexPath, animated: true)
        }
    }
    

  • answered 2019-11-14 10:35 Trai Nguyen

    Im not sure unwind in your case mean but in case turn app to background and open app again, code don't run into viewWillAppear function.

    Try put the debug sign in viewWillAppear in your code and check it again.

    If I correct, you should check which view controller is presenting in AppDelegate and call deselectRow of your table

  • answered 2019-11-14 13:03 vadian

    From the documentation of deselectRow(at:animated:)

    Calling this method does not cause the delegate to receive a tableView(_:willDeselectRowAt:) or tableView(_:didDeselectRowAt:) message, nor does it send selectionDidChangeNotification notifications to observers.

    Calling this method does not cause any scrolling to the deselected row.

    A solution is to move the code in didDeselectRowAt into an extra method

    func deselectRowAnimated(at indexPath : IndexPath)
    {
        switch indexPath.section {
        case 2:
            let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
            UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
                cell.folderTagSelectionBKG.alpha = 0
            })
        default:
            let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
            UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
                cell.folderSelectionBKG.alpha = 0
            })
        }
    }
    

    and call it in viewWillAppear and didDeleselect

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //Deselect row on unwind
        if let indexPath = folderTableView.indexPathForSelectedRow {
            deselectRowAnimated(at: indexPath)
        }
    }
    
    ...
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)  {
        deselectRowAnimated(at: indexPath)
    }
    

    selectrow