简体   繁体   中英

Displaying action sheet when tableview cell is tapped

Then I am trying to call action sheet when cell is tapped, and this is what I did

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

        let alertController = UIAlertController(title: "Action Sheet", message: "What do you like to do", preferredStyle: .alert)

        let okButton = UIAlertAction(title: "Done", style: .default, handler: { (action) -> Void in
            print("Ok button tapped")
        })

        let deleteButton = UIAlertAction(title: "Skip", style: .destructive, handler: { (action) -> Void in
            print("Delete button tapped")
        })

    alertController.addAction(okButton)
}

When i am tapping cell, alert controller is not showing up. What am I missing?

你几乎没有,请确保你添加deleteButton -动作以及和本alertController使用present(alertController, animated: true, completion: nil)

您的操作表未显示,因为您未在显示它。

present(alertController, animated: true /** or false */, completion: nil)

We create a function didSelectContact , and use func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) to handle selected Cell in tableview. If each cell tapped this action sheet will open.

func didSelectContact(){

    let alert = UIAlertController(title: "Choose Once", message: "What you want do with contact", preferredStyle: UIAlertControllerStyle.actionSheet)
    let call = UIAlertAction(title: "Call", style: UIAlertActionStyle.default) { (UIAlertAction) in
    }
    let sms = UIAlertAction(title: "SMS", style: UIAlertActionStyle.default) { (UIAlertAction) in

    }
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

    alert.addAction(call)
    alert.addAction(sms)
    alert.addAction(cancel)

    self.present(alert, animated: true, completion: nil)
}

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

    self.didSelectContact()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM