美好的一天!
我有一个具有 EditingStyle 的 TableViewController:
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
}
}
每个单元格都有一个带有行号的标签。如果我 .reloadData() 如上面的代码所示,它会破坏 .deleteRows() 动画。我尝试了不同的变体,使用 beginUpdates() 和 .reloadRows(),没有给出所需的结果。我认为有一个简单的解决方案,但我无法弄清楚。
编辑:
编辑代码,以便该项目首先从数组中删除,然后从 tableView 中删除。
示例:
如果删除第 5 行,如何使用 .reloadData() 使一切正常。我的意思是不会有1-2-3-4-6-7-8-9-10。如何在不破坏 .Delete 动画的情况下重新加载它?
最佳答案
只需从代码中删除 tableView.reloadData()
即可,不需要。
您的重写方法应该是
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)
} 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
}
}
关于swift - 在 TableView 中删除后更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52036685/