delete a row in table view in swift

前端 未结 9 1732
萌比男神i
萌比男神i 2021-02-05 22:38

I am trying to delete a row in table view. I have implemented the required methods but when i am swiping the row horizontally no delete button is coming. I have searched and I h

相关标签:
9条回答
  • 2021-02-05 22:58

    If below coding is helpful for you,i will be happy

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
    {
        return true
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
    {
         if editingStyle == .Delete 
         {
            yourArray.removeAtIndex(indexPath.row) 
            self.tableView.reloadData()   
         }
    }
    

    SWIFT 3.0

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
    {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
    {
       if editingStyle == .delete
       {
          yourArray.remove(at: indexPath.row)
          tblDltRow.reloadData()
       }
    }
    

    You have to refresh the table.

    0 讨论(0)
  • 2021-02-05 23:03

    I struggled with this as well but finally found out a solution - I am running XCode 8.3.2. A lot of the answers I read were for older versions of XCode / Swift and many were for Objective-C.

    The solution I found was to use the 'editActionsForRowAt' tool for UITableViews. Note: Everything else I read kept pointing me towards the 'commit editingStyle' tool for UITableView, but I could never get it to work. Using editActionsForRowAt worked perfectly for me.

    NOTE: 'postData' is the name of the Array that I added data to.

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
        let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
            // Remove item from the array
            postData.remove(at: IndexPath.row)
    
            // Delete the row from the table view
            tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
            })
            // set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish)
            deleteAction.backgroundColor = UIColor.red
    
        return [deleteAction]
    }
    

    I also have some code to add an 'EDIT button' alongside the Delete button:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
        let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in
            //print("Edit tapped")
            })
            // Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue)
            editAction.backgroundColor = UIColor.blue
    
        let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
            //print("Delete tapped")
            // Remove item from the array
            postData.remove(at: IndexPath.row)
    
            // Delete the row from the table view
            tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
            })
            // set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box)
            deleteAction.backgroundColor = UIColor.green
    
        return [editAction, deleteAction]
    }
    

    EDIT: fixed format so code showed up in the grey box :)

    0 讨论(0)
  • 2021-02-05 23:03
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
    {
    return true
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
     {
     if editingStyle == .Delete 
     {
        carsArray.removeAtIndex(indexPath.row) 
        self.tableView.reloadData()   
     }
     }
    
    0 讨论(0)
提交回复
热议问题