Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete a row in table view in swift

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 have found the same solution everywhere but it is not working in my case. I don't know where i am making mistake. Can anyone help me please?

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{
     if editingStyle == .Delete 
     {
        dataHandler.deletePeripheral(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     }
}
like image 690
Sunil Kumar Avatar asked Mar 27 '15 05:03

Sunil Kumar


People also ask

How do you delete a row in Swift?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

How do I swipe to delete Uitableviewcells?

When you want to handle deleting, you have to do three things: first, check that it's a delete that's happening and not an insert (this is down to how you use the UI); second, delete the item from the data source you used to build the table; and third, call deleteRows(at:) on your table view.

How do I delete a simple Tableview in swift 5?

After that, we need to add the row into the table view at index path 0 and remove the values from the input textfield. Run the application and you can add the cell when you tap on Add Button. Now, we will Delete the cell from the tableview when use click on the Delete button on the cell.


2 Answers

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

like image 194
Geoffrey Waterson Avatar answered Sep 21 '22 18:09

Geoffrey Waterson


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.

like image 20
user3182143 Avatar answered Sep 21 '22 18:09

user3182143