Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete table row with animation

I want to delete a row with animation in my table view controller. I use the following code:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == .Delete) {

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

        let LM_ITEM = lebensmittel[indexPath.row]
        managedObjectContext?.deleteObject(lebensmittel[indexPath.row])
        self.DatenAbrufen()
    }
}

but after press on delete, I get this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x1856382d8 0x1973040e4 0x185638198 0x1864eced4 0x18a296e5c 0x10010e278 0x10010ef9c 0x18a2b0ea4 0x18a3a6880 0x18a0e5398 0x18a0ce474 0x18a0e4d34 0x18a0a3f54 0x18a0de82c 0x18a0ddee4 0x18a0b1120 0x18a3522b8 0x18a0af634 0x1855f0240 0x1855ef4e4 0x1855ed594 0x1855192d4 0x18ef6f6fc 0x18a116f40 0x100134420 0x1979aea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

like image 928
Ghost108 Avatar asked Jul 12 '15 06:07

Ghost108


People also ask

How do I animate a table in Excel?

Selection handles will appear on all the individual cells in the table. Click outside the table to clear these selections. Press and hold the Ctrl key while you select the parts of the table that you want to animate. On the Animations tab, in the Advanced Animations group, click Add Animation to open the menu of animation options:

How do I delete a row from a table?

The deleteRow () method removes the row at the specified index from a table. Tip: Use the insertRow () to create and insert a new row. Required in Firefox and Opera, optional in IE, Chrome and Safari.

How to delete items in Tabel view cell?

When a user slides horizontally across a row the editing style of the Tabel View Cell is set to delete. When the delete button is pressed, the item is deleted in the array and also the row is deleted in the Table View.

How to delete a row from a table view in iOS?

Inside a Table View the rows can be manipulated by user actions. In this tutorial we will delete a row from a Table View using the "swipe-to-delete" gesture. The row will be deleted in the data model (an array) and also inside the Table View itself. This tutorial is made with Xcode 10 and built for iOS 12.


2 Answers

You need to update your model before call tableView.deleteRowsAtIndexPaths(..)

like this,

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == .Delete) {

        let LM_ITEM = lebensmittel[indexPath.row]
        managedObjectContext?.deleteObject(lebensmittel[indexPath.row])
        self.DatenAbrufen()

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

Swift 5

tableView.deleteRows(at: [indexPath], with: .automatic)
like image 167
nRewik Avatar answered Oct 11 '22 12:10

nRewik


Use this for Swift 4

tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)

it will delete the tableView row with animation similar to the iPhone messaging application.

like image 23
Ummar Ahmed Avatar answered Oct 11 '22 12:10

Ummar Ahmed