Reputation: 110
When deleting a cell from the tableView, the animation runs fine, but the tableView data isn't reloaded. I tried running without .beginUpdates()
and .endUpdates()
and instead used .reloadData()
, and the process worked but the animation didn't work.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
switch editingStyle {
case .Delete:
// remove the deleted item from the model
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
context.deleteObject(coreData[indexPath.row] as NSManagedObject)
coreData.removeAtIndex(indexPath.row)
do {
try context.save()
} catch {
print("Error deleting NSManagedObject")
}
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tasks.removeAtIndex(indexPath.row)
names.removeAtIndex(indexPath.row)
ids.removeAtIndex(indexPath.row)
tableView.endUpdates()
// remove the deleted item from the `UITableView`
default:
return
}
}
By the way, the tasks
array is the the main data for the tableView.
How can I reload the data as I delete a cell, WITH the smooth deletion animation?
Upvotes: 1
Views: 603
Reputation: 318934
Two things to consider:
You must update the data model before you update the table view. Therefore the order of the code must be:
tasks.removeAtIndex(indexPath.row)
names.removeAtIndex(indexPath.row)
ids.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
There is no need for the calls to begin/endUpdates
since you only make a single call to update the table view.
Upvotes: 4
Reputation: 9943
Move all the removeAtIndex...
lines before the beginUpdates
, u have to remove the object from the data first before u can delete row
Upvotes: 2