Deleting a Row from a UITableView in Swift 3?

前端 未结 8 1262
难免孤独
难免孤独 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 09:16

    **

    for swift 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)
          }
        }
    
    0 讨论(0)
  • 2020-11-28 09:19

    You don't have to reload the tableView.

    Before you use tableView.deleteRows(at: [IndexPath], with: UITableView.RowAnimation) you must substrate the number of rows you want delete in numberOfRowsInSection

        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return //Your current number of rows - The number of rows to delete
    
        }
    

    Normally the number of rows is the number of Items from an array (yourArray.count), that means you can delete the items from this array before you delete the cells and you get the same result.

    0 讨论(0)
提交回复
热议问题