Add swipe to delete UITableViewCell

后端 未结 25 1579
暖寄归人
暖寄归人 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]
    }
    

    0 讨论(0)
  • 2020-11-30 18:09

    Add these two functions:

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            // handle delete (by removing the data from your array and updating the tableview)
        }
    }
    

    Swift 3.0:

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {
            // handle delete (by removing the data from your array and updating the tableview)
        }
    }
    

    Swift 4.2

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            // handle delete (by removing the data from your array and updating the tableview)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:09

    just add these assuming your data array is 'data'

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {
            // handle delete (by removing the data from your array and updating the tableview)
            if let tv=table
            {
    
    
    
                data.remove(at: indexPath.row)
                tv.deleteRows(at: [indexPath], with: .fade)
    
    
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:10

    Swift 3:

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {
            // delete data and row
            dataList.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:10

    In Swift 4 tableview add, swipe to delete UITableViewCell

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in
            // delete item at indexPath
    
        }
        return [delete]
    }
    
    0 讨论(0)
  • 2020-11-30 18:13

    Swift 3 with custom title supported

            func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
                    return true
                }
    
        //If you want to change title
                func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
                    return "Cancel"
                }
    
                func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
                    if (editingStyle == UITableViewCellEditingStyle.delete) {
    // you might want to delete the item at the array first before calling this function
                        tableView.deleteRows(at: indexPath, with: .automatic)
                    }
                }
    
    0 讨论(0)
提交回复
热议问题