从Swift 3中的UITableView删除一行?

问题描述 投票:29回答:6

我是编程和学习swift的新手,我正在学习swift 2的教程并使用swift 3,因此我跟随时会遇到一些问题,这是一个我已经适当坚持的问题。

我有一个名称表,我正在为它们制作一个滑动和删除功能,它将它们从名称变量中删除,这是一个数组。我在xcode中选择了与教程最相似的函数并填写了它们,但是当我点击删除按钮时,我的应用程序随机崩溃。这是删除按钮的代码...

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in

        print("Deleted")

        self.catNames.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        self.tableView.reloadData()
    }
swift uitableview row
6个回答
89
投票

适用于Swift 3和Swift 4

使用UITableViewDataSource tableView(:commit:forRowAt:)方法,另请参阅此答案here

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
    print("Deleted")

    self.catNames.remove(at: indexPath.row)
    self.tableView.deleteRows(at: [indexPath], with: .automatic)
  }
}

20
投票

首先,您需要添加此功能

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true 
}

然后你的功能还可以,但不需要tableview重载数据只需调用tableview.beingUpdatestableview.endUpdates

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
       print("Deleted")
       self.catNames.remove(at: indexPath.row)
       self.tableView.beginUpdates()
       self.tableView.deleteRows(at: [indexPath], with: .automatic)
       self.tableView.endUpdates() 
    }
}

6
投票

在Swift 4中尝试这个

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    func tableView(_ tableView: UITableView, commit editingStyle:   UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            arrStudentName.remove(at: indexPath.row)
            tableView.beginUpdates()
            tableView.deleteRows(at: [indexPath], with: .middle)
            tableView.endUpdates()
        }
    }

3
投票
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {

        self.animals.remove(at: indexPath.row)

        self.dataDisplayTbleView.reloadData()
    }

}

1
投票

我的回答

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
     return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    if editingStyle == .delete
    {
        array.remove(at: indexPath.row)
        tblDltRow.reloadData()
    }
}

你需要刷新表


1
投票

**

对于斯威夫特3

**

  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
      if editingStyle == .delete {
        print("Deleted")

        self.catNames.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.