swift - 在 TableView 中删除后更新行

标签 swift uitableview delete-row editing

美好的一天!

我有一个具有 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 中删除。

示例:

enter image description here

如果删除第 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/

相关文章:

arrays - 自定义单元格在 TableView 中全白

嵌入式 WebView OSX 中未显示 Javascript 警报

ios - UITableViewCell 3 选择的时间更改多个选择的背景颜色

ios - UITableViewCells 之间的间距

mysql - DELETE - INNER JOIN - 子查询

Android sqlite delete 实际上并没有删除行

iOS:在主从应用程序的导航栏中添加搜索栏

ios - 意外的零窗口(空白黑屏)

ios - 如何制作带有圆角和单线边框的缩进 UITableViewCell

php - 使用复选框删除多个 MySQL 项目