Update Rows after .Delete in TableView

Clash Royale CLAN TAG#URR8PPP
Update Rows after .Delete in TableView
Good day!
I have a TableViewController with EditingStyle:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == .delete
machine.formulas.remove(at: indexPath.row)
machine.saveFormulas()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
else if editingStyle == .insert
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
Each cell has a label with row number. If i .reloadData() as in the code above it breaks the .deleteRows() animation. I tried different variations, with beginUpdates() and .reloadRows(), nothing gave the required result. I think there is a simple solution for that, but I can't figure it out.
EDIT:
Edited code so the item is removed from array at first then from tableView.
EXAMPLE:

If you delete row #5, how do you .reloadData() so everything is in order. I mean there won't be 1-2-3-4-6-7-8-9-10. And how do you reload it without breaking .Delete animation?
.reloadData()
.deleteRows
Each cell has a label with row number. After TableView does it animation on removing a row, one disappear and all others move together the information on the rows themselves don't get updated.
– Konstantin
35 mins ago
2 Answers
2
Simply remove from the dataSource and Delete the row can do, No need to reload it.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == .delete
machine.formulas.remove(at: indexPath.row)
machine.saveFormulas()
tableView.deleteRows(at: [indexPath], with: .fade)
self.perform(#selector(reloadTable), with: nil, afterDelay: 2)
else if editingStyle == .insert
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
@objc func reloadTable()
self.tableView.reloadData()
Try and share results.
Animation is fine, but still rows are not updated after delete. i.stack.imgur.com/AVClM.png
– Konstantin
37 mins ago
What you mean by "but still rows are not updated after delete" ?, you can see in the screenshot, the right image has one less row and it's deleted row with title b. can you explain, what else you are looking for?
– Bhavin Kansagara
34 mins ago
Each row has a label with row number. Left side of each cell. They don't get updated, they used to be 1-2-3-4, then 2 row gets deleted, and they still go as 1-3-4 instead of 1-2-3
– Konstantin
32 mins ago
deleting rows, simply removes one row from the index, it will not reload the rows. You should reload the table in place of delete a single row, to get the required results.
– Bhavin Kansagara
26 mins ago
That is what I am asking, since .deleteRows does not have any completion handler, how can i track its animation to be finished so i can .ReloadRows without interrupting?
– Konstantin
15 mins ago
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == .delete
machine.formulas.remove(at: indexPath.row)
machine.saveFormulas()
tableView.deleteRows(at: [indexPath], with: .fade)
self.perform(#selector(reloadTable), with: nil, afterDelay: 2)
@objc func reloadTable()
DispatchQueue.main.async //please do all interface updates in main thread only
self.tableView.reloadData()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why using
.reloadData()? Only.deleteRowswill do that for you. Also remove data from array first then delete the row from table.– Amit
1 hour ago