Deleting a Row from a UITableView in Swift 3?

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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'm properly stuck on.

I have a table of names and i am making a swipe and delete function for them which removes them from a names variable which is an array. I selected the functions which most closely resembled the tutorial inside xcode and filled them out, but my app crashes randomly when I click the delete button. Here is my code for the delete button...

    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()     }

回答1:

Works for Swift 3 and Swift 4

Use the UITableViewDataSource tableView(:commit:forRowAt:) method, see also this answer 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)   } }


回答2:

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()      } }


回答3:

In Swift 4 Try This

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()         }     }


回答4:

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


回答5:

My answer for you

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()     } }

You need to refresh the table



回答6:

**

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)       }     }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!