繁体   English   中英

从tableView删除行的正确方法是什么?

[英]What is the correct way to remove a row from a tableView?

在Xcode 9中,我有一个tableView,其中包含从CoreData中提取的数据列表。 数据的添加,编辑和保存工作正常,但是当我尝试删除数据时,我遇到了一个难题,一个难题是美观,而另一个则使应用程序崩溃。

如果我使用以下代码,它将从CoreData和tableview中删除数据,但没有.fade选项。 它只是从tableView中消失了。

  override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
         context.delete(tasks[indexPath.row])
         tasks.remove(at: indexPath.row)
         saveTasks()
  }

如果我使用以下选项,则应用程序崩溃。

  override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
         context.delete(tasks[indexPath.row])
         tableView.deleteRows(at: [indexPath], with: .fade)
         saveTasks()
  }

我如何将.fade选项应用于删除位置:?

您需要结合这两种解决方案:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    context.delete(tasks[indexPath.row])
    tasks.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
    saveTasks()
}

基本上,您既必须更新数据( tasks ),又要告诉tableView重新加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM