Vah.Sah
Vah.Sah

Reputation: 532

Action Sheet Takes 10+ seconds to appear

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?

Upvotes: 3

Views: 1072

Answers (1)

Khalid Afridi
Khalid Afridi

Reputation: 923

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

    }
}

Upvotes: 9

Related Questions