简体   繁体   English

轻按Tableview单元格时显示操作表

[英]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. 我们创建一个函数didSelectContact ,并使用func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)处理表func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)选定单元格。 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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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