0

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
        })
    }
}
emrcftci
  • 3,355
  • 3
  • 21
  • 35
mink23
  • 144
  • 1
  • 12
  • So you want to clear all the selected cell when view is appear. – Krunal Nagvadia Nov 14 '19 at 05:05
  • 1
    if you want programatically fire didDeselectRowAt you should use myTableView.delegate?.tableView?(myTableView, didDeselectRowAt: indexpath) in viewWillApper – Bhavesh.iosDev Nov 14 '19 at 06:19
  • 1
    @Bhavesh.iosDev You are strongly discouraged from calling any delegate method including `did`, `will` and `should` yourself. – vadian Nov 14 '19 at 13:04

3 Answers3

2

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

Community
  • 1
  • 1
vadian
  • 274,689
  • 30
  • 353
  • 361
0

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

Jiffin
  • 405
  • 5
  • 8
  • Hi, I'm not using a View Controller and I currently have the code in viewDidAppear but it doesn't call 'didDeselectRowAt' – mink23 Nov 14 '19 at 05:54
0

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)
    }
}
Aira Samson
  • 226
  • 2
  • 10