Add swipe to delete UITableViewCell

后端 未结 25 1626
暖寄归人
暖寄归人 2020-11-30 17:50

I am making a CheckList application with a UITableView. I was wondering how to add a swipe to delete a UITableViewCell.

This is my ViewCont

25条回答
  •  情话喂你
    2020-11-30 18:08

    Simply add method:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let delete = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "Delete") { (action, indexPath) in
            self.arrayFruit.remove(at: indexPath.row)
            self.tblList.reloadData()
        }
    
        let edit = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Edit") { (action, indexpath) in
    
            let alert = UIAlertController(title: "FruitApp", message: "Enter Fuit Name", preferredStyle: UIAlertControllerStyle.alert)
            alert.addTextField(configurationHandler: { (textField) in
                textField.placeholder = "Enter new fruit name"
            })
            alert.addAction(UIAlertAction(title: "Update", style: UIAlertActionStyle.default, handler: { [weak alert](_) in
                let textField = alert?.textFields![0]
                self.arrayFruit[indexPath.row] = (textField?.text!)!
                self.tblList.reloadData()
            }))
    
            self.present(alert, animated: true, completion: nil)
        }
        edit.backgroundColor = UIColor.blue
        return [delete,edit]
    }
    

提交回复
热议问题