使用破坏性样式和 UIAlertAction 删除按钮

delete button using destructive style and UIAlertAction

我是 swift 的新手,我正在开发一个联系人应用程序。我希望能够在用户单击 delete 按钮时删除我拥有的联系人,但我无法实现它。目前,我有一个功能可以让我在用户单击 Show Details 时看到 contact,并且还有一个 cancel button。但是对于我的删除,我希望能够摆脱我点击的任何联系。我会把代码贴在下面。

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedContact = contacts[indexPath.row] as? Contact
        let name = selectedContact!.contactName!
        let actionHandler = { (action:UIAlertAction!) -> Void in
            //            self.performSegue(withIdentifier: "EditContact", sender: tableView.cellForRow(at: indexPath))
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "ContactController")
                as? ContactsViewController
            controller?.currentContact = selectedContact
            self.navigationController?.pushViewController(controller!, animated: true)
        }

        let alertController = UIAlertController(title: "Contact selected",
                                                message: "Selected row: \(indexPath.row) (\(name))",
            preferredStyle: .alert)

        let actionDelete = UIAlertAction(title: "Delete" ,
                                   style: .destructive,
                                   handler:nil)

        let actionCancel = UIAlertAction(title: "Cancel",
                                         style: .cancel,
                                         handler: nil)
        let actionDetails = UIAlertAction(title: "Show Details",
                                          style: .default,
                                          handler: actionHandler)
        alertController.addAction(actionCancel)
        alertController.addAction(actionDetails)
        alertController.addAction(actionDelete)
        present(alertController, animated: true, completion: nil)
    }

您可以对所有 with/out 操作尝试此操作,因为无需创建 var 然后添加它,您可以 addAction 直接使用它的处理程序

alert.addAction(UIAlertAction(title: "Delete", style:.destructive, handler: { (action) in 

      contacts.remove(at:indexPath.row) 
      tableView.deleteRows(at:[indexPath],with:.fade)

}))

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedContact = contacts[indexPath.row] as? Contact
    let name = selectedContact!.contactName! 
    let alertController = UIAlertController(title: "Contact selected",
                                            message: "Selected row: \(indexPath.row) (\(name))",
        preferredStyle: .alert)


     alertController.addAction(UIAlertAction(title: "Show Details", style:.default, handler: { (action) in 

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "ContactController")
            as? ContactsViewController
        controller?.currentContact = selectedContact
        self.navigationController?.pushViewController(controller!, animated: true)

    }))

    alertController.addAction(UIAlertAction(title: "Delete", style:.destructive, handler: { (action) in 

        contacts.remove(at:indexPath.row) 
        tableView.deleteRows(at:[indexPath],with:.fade)

    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style:.cancel, handler:nil))  

    present(alertController, animated: true, completion: nil)
}