iosswiftuitableviewuitableviewrowaction

How to add a slight delay before triggering didSelectRowAt action and deselecting the tabelview cell


Edit: I didn't realize that it is the Animates (in segue in storyboard) and animated in pushViewController() which does the job.

How to smooth transition to next view when we tap on a row in table view. I want to achieve similar smooth transitioning effect as Apple's Settings app. Right now my transition happens immediately as soon as row is touched.

On Apple's settings app when you tap on a row on Settings menu, it first highlights the row then smoothly transitions to next view. This is what I want to achieve.

I've searched but can't find any relevant solution. Any help?

Following is my code for row select

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    tableView.deselectRow(at: indexPath, animated: true)
    
    //.....
    self.navigationController?.pushViewController(destinationVC, animated: false)
}

Solution

  • You need to change the presentation style. So click on your segue in the storyboard. Then select Kind as Push from the dropdown in Attributes Inspector on the right side.

    Also, to present it programmatically, you need to set an identifier in the first text field in Attributes Inspector. After that in your tableView(_:didSelectRowAt:) add this.

    performSegue(withIdentifier: "SegueIdentifier", sender: nil)
    

    If you want a delayed action then you can use asyncAfter, like suggested here.

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.33) {
       self.deselectRow(at: indexPath, animated: true)
       self.performSegue(withIdentifier: "SegueIdentifier", sender: nil)
    }