簡體   English   中英

輕按Tableview單元格時顯示操作表

[英]Displaying action sheet when tableview cell is tapped

然后我嘗試在輕按單元格時調用操作表,這就是我所做的

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)
}

當我點擊單元格時,警報控制器未顯示。 我想念什么?

你幾乎沒有,請確保你添加deleteButton -動作以及和本alertController使用present(alertController, animated: true, completion: nil)

您的操作表未顯示,因為您未在顯示它。

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

我們創建一個函數didSelectContact ,並使用func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)處理表func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)選定單元格。 如果點擊了每個單元格,則此操作表將打開。

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