Difference between tableView.deleteRowAtIndexPaths() and tableView.reloadData()

In the checklist tutorial I found that we are using below code to delete cell from tableView

if editingStyle == UITableViewCellEditingStyle.Delete { lists.removeAtIndex(indexPath.row) let indexPaths = [indexPath] tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) }

I am interested to know what if we simply remove the item from the list and reload the tableView? i.e
if editingStyle == UITableViewCellEditingStyle.Delete { lists.removeAtIndex(indexPath.row) tableView.reloadTable() }

What are the benefit of approach 1 over approach 2?

This actually refreshes the table view to reflect the change in datasource data.

yeah thats correct reloadTable() refreshes the table view to reflect the change in datasource, but specific to this case both reloadTable() and tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) does the same thing. So my question is which one is better and why?

tableView.deleteRowsAtIndexPaths() shows an animation for when you delete the row. You may not see this animation when the deletion happens when you go back from one screen to another, but you’ll definitely see it when it happens on the same screen. With reloadData() there is no animation.

1 Like

Thank you!!

Thats what I also thought.


reloadRowsAtIndexPaths (which needs endUpdates to reload) returns nil when dequeueReusableCellWithIdentifier is called.
reloadData reuses the cell which was already allocated.

Another difference is that endUpdates after reloadRowsAtIndexPaths directly calls cellForRowAtIndexPath whereas reloadData schedules the calls to cellForRowAtIndexPath at a later time so it’s not immediate.