从Swift 3中的UITableView删除一行?

我是编程和学习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 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)
  }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...