iOS - Delete row from UITableView crash - ios

I am trying to delete row from table view and core data the following way:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
managedObjectContext?.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)
managedObjectContext?.save(nil)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.reloadData()
}
}
But on self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) my application crashes with the following 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 (7) must be equal to the number of rows contained in that section before the update (7), 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).'
Maybe someone had something like this. Can anyone help me?

After you delete a row, your function numberOfRowsForSection is called again.
So by the time you delete the row, you should also update the value that you return in the aforementioned function.

Related

UITableView: Invalid update: invalid number of rows in section 0 [duplicate]

Swift 3.0 iOS 10.x
Using this code to try and delete a row in a table...
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
print("DELETE \(indexPath)")
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
This fails with the error message?
2017-05-29 13:36:23.843228+0200[939:576777] *** 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 (9) must be equal to the number of rows contained in that section before the update (9), 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).'
Which sounds fair enough only this is boiler plate code? I am doing little more than clicking on the red button?
Yes, there are 3 rows here... in my code that crashed there were 9.
What have I missed here? Printed out the returned indexPath here and indeed it was wrong, but wait I didn't set it. This method did?
DELETE [0, 3]
You must delete row in your data array before deleting in tableView
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
print("DELETE \(indexPath)")
yourArray.remove(at: indexPath.row) /* delete in data array */
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}

NSInternalInconsistencyException fix

Updated method:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
initialData[indexPath.section].remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else {
}
}
Error:
reason: 'Invalid update: invalid number of rows in section 0. The
number of rows contained in an existing section after the update (2)
must be equal to the number of rows contained in that section before
the update (2), 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).
How can I fix this?
Read what your console is telling you very carefully.
Your data source is complaining that after you removed a row with removeAtIndexPaths() it doesn't have the expected number of rows.
Make sure you maintain integrity in what you delete/ add and the data source changing accordingly as well. Also, make sure to make these changes on the main queue.
Good luck! :)

Delete cell from tableview

I am using core data to populate a table view cell. I am trying to implement a function to delete the cell however I keep getting this error
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:1610
2017-07-15 22:07:51.077 Annoying Alarm- Alarm can't be snoozed or stopped![7608:793341] *** 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),
My code for view controller is as follows
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let cell = tableView.cellForRow(at: indexPath) as! AlarmCell
let identifier = cell.time.text
for object in self.controller.fetchedObjects!{
if object.timeTitle == identifier {
print("HELLO!")
print("deep inside if")
context.delete(object)
ad.saveContext()
tableView.deleteRows(at: [indexPath], with: .fade)
//tableView.reloadData()
}
}
}
}
The core date part works just fine and when I reopen the app the cell does disappear. My problem is when I actually press delete the
tableView.deleteRows(at: [indexPath], with: .fade)
function throws the error displayed above. When I comment it out, no crash occurs but the cell is not deleted unless I go to another view then come back to this one.
error indicates that after deleting the row you should remove it from your array and update your array. then you should reload your tableview so your tableviews numberofrows get updated.

NSInternalInconsistencyException tableView row deletion

Swift 3.0 iOS 10.x
Using this code to try and delete a row in a table...
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
print("DELETE \(indexPath)")
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
This fails with the error message?
2017-05-29 13:36:23.843228+0200[939:576777] *** 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 (9) must be equal to the number of rows contained in that section before the update (9), 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).'
Which sounds fair enough only this is boiler plate code? I am doing little more than clicking on the red button?
Yes, there are 3 rows here... in my code that crashed there were 9.
What have I missed here? Printed out the returned indexPath here and indeed it was wrong, but wait I didn't set it. This method did?
DELETE [0, 3]
You must delete row in your data array before deleting in tableView
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
print("DELETE \(indexPath)")
yourArray.remove(at: indexPath.row) /* delete in data array */
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}

TableView row removal with NSFetchedResultsController crashing?

I'm trying to let users delete a row from a UITableView that's populated using a NSFetchedResultsController but keep getting a fatal error:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Invalid update: invalid
number of rows in section 1. The number of rows contained in an
existing section after the update (7) must be equal to the number of
rows contained in that section before the update (7), 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).'
I'm deleting the core data entry and then deleting the row in the tableView in my code but still get the error above:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
self.feedTable.beginUpdates()
let event = self.fetchedResultsController.object(at: indexPath) as Event
self.managedObjectContext.delete(self.fetchedResultsController.object(at: indexPath))
do {
try self.managedObjectContext.save()
} catch {}
self.feedTable.deleteRows(at: [indexPath], with: .automatic)
self.feedTable.endUpdates()
}
}
Can anybody help understand what I'm doing wrong?
If you use a fetched results controller. Only delete the object with
self.managedObjectContext.delete(self.fetchedResultsController.object(at: indexPath))
Try this solution
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let event = self.fetchedResultsController.object(at: indexPath) as Event
self.managedObjectContext.delete(self.fetchedResultsController.object(at: indexPath))
do {
try self.managedObjectContext.save()
} catch {}
}
}
You don't need beginUpdates and endUpdates

Resources