Deleting a Row from a UITableView in Swift 3?

前端 未结 8 1330
难免孤独
难免孤独 2020-11-28 08:31

I\'m new to coding and learning swift, I am following a tutorial for swift 2 and working with swift 3 so there are a few issues I have when following along, this being one I

8条回答
  •  失恋的感觉
    2020-11-28 08:56

    First you need to add this function

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

    then your function is ok but no need of tableview reload data just call tableview.beingUpdates and tableview.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() 
        }
    }
    

提交回复
热议问题