3

In my app I have table view controller. When the user types on the last row of tableview the action sheet should appears to ask for sign out. Here is my code for this action:

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

        switch indexPath.row {
        case 0:
            //..
        case 1:
            //..
        case 2:
            //..
        case 3:

            let logOutMenu = UIAlertController(title: nil, message: "Are you sure want to logout?", preferredStyle: .actionSheet)

            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            let logOutAction = UIAlertAction(title: "Log out", style: .default, handler: { (UIAlertAction) in
                print("sign out")
            })

            logOutMenu.addAction(cancelAction)
            logOutMenu.addAction(logOutAction)

            self.present(logOutMenu, animated: true, completion: nil)
        default: break

        }
    }

Everything works fine, however there is strange behaviour of action sheet. It takes approximately 10 seconds (or even more) to show action sheet. The same behaviour I have noticed on real device as well. What I'm doing wrong?

Vah.Sah
  • 522
  • 1
  • 6
  • 21
  • 3
    "Slow to happen" UI updates often indicates you're doing something off the main thread. Put a breakpoint on the `present`and check what thread it's running on. – Ashley Mills Apr 06 '17 at 11:49
  • @AshleyMills good point. – George Vardikos Apr 06 '17 at 11:59
  • I have put breakpoint on the present, and on the main thread the running method is tableView(UITableView, didSelectRowAt indexPath: IndexPath). – Vah.Sah Apr 06 '17 at 12:03
  • After adding "tableView.deselectRow(at: indexPath, animated: false)" as was suggested by @Khalid Afridi the problem was solved. Maybe the problem was that. – Vah.Sah Apr 06 '17 at 12:06
  • Your code seems good there is not a problem on the block that you have shared at least. You should probably look elsewhere. Check out the accepted answer on this [question](http://stackoverflow.com/questions/11030114/uiactionsheet-taking-long-time-to-respond) If you handling some previous block code in a background thread you might need to take a look at this [post](https://forums.macrumors.com/threads/uiactionsheet-takes-20-seconds-to-appear.1524025/) which seems to face a similar issue with you. – George Vardikos Apr 06 '17 at 11:36

1 Answers1

9

You have to call deselect row at index path without animation otherwise there two animations at the same time which confuses and gets longer time

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

    switch indexPath.row {
    case 0:
        //..
    case 1:
        //..
    case 2:
        //..
    case 3:

        let logOutMenu = UIAlertController(title: nil, message: "Are you sure want to logout?", preferredStyle: .actionSheet)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let logOutAction = UIAlertAction(title: "Log out", style: .default, handler: { (UIAlertAction) in
            print("sign out")
        })

        logOutMenu.addAction(cancelAction)
        logOutMenu.addAction(logOutAction)

        self.present(logOutMenu, animated: true, completion: nil)
       // Deselect your row it will fix it
       tableView.deselectRow(at: indexPath, animated: false)
    default: break

    }
}
Khalid Afridi
  • 913
  • 5
  • 12