Delete cell from tableview - ios

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.

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

Deleting UITableViewCell causing crash

I know there's a number of posts on this subject and I've been through most of them but I still can't solve an issue I'm having when deleting a tableViewCell causing my app to crash with the follow error:
Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})'
I explicitly set the height of my rows using
tableView.rowHeight = 140
My code for the delete is as follows
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
let object = self.datastore[indexPath.row]
do {
try self.realm?.write {
self.realm?.delete(object)
self.datastore.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .none)
}
} catch let error {
}
}
deleteAction.backgroundColor = .red
return [deleteAction]
}
This does delete the item correctly from the dataStore. I've tried a number of different solutions here such as calling the delete on the mainThread, reloading the whole dataSource etc. but it still crashes.
My numberOfRowsInSection method gets called and is return the correct number of rows after the deletion so that doesn't appear to be the issue.
Adding a general exception breakpoint though I think it's crashing here
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? Cell {
cell.image.kf.cancelDownloadTask()
}
}
kf here is the kingfisher library as I want to cancel an image download if the cell is removed
What I have noticed is that if I use
`tableView.dequeueReusableCell(withIdentifier: "Cell")`
instead of
tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
This doesn't crash, so that might be my solution, but I'm eager to understand what's wrong here. I understand that the 'forIndexPath' method should always return a cell but in this instance is crashing because of a nil.
I have the same deletion code in another viewController that doesn't rely on the didEndDisplaying:Cell method and this doesn't crash
You need to change this line in didEndDisplaying. you need to get existing cell.
let cell = tableView.cellForRowAtIndexPath(indexPath)
I had a very similar issue.
Make sure that when you're deleting your cell, your goal is not to remove the entire section. My tableView is built on a varying length of sections instead of rows.
I found that by replacing
tableView.deleteRows(at: [indexPath], with: .automatic
with:
tableView.deleteSections(IndexSet(integer: indexPath.section), with: .automatic)
I was able to get fix the bug. Make sure you want to delete the entire section however before you implement this change!
Hope this helps, cheers :)

iOS - Delete row from UITableView crash

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.

Resources