Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a Row from a UITableView in Swift?

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

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.

like image 554
infernouk Avatar asked Oct 20 '16 13:10

infernouk


People also ask

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

How do I delete a row from a table view?

When a user slides horizontally across a row the editing style of the Tabel View Cell is set to delete. When the delete button is pressed, the item is deleted in the array and also the row is deleted in the Table View. Build and run the project and swipe-to-delete a row from the Table View.


2 Answers

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)   } } 
like image 145
ronatory Avatar answered Oct 04 '22 04:10

ronatory


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()      } } 
like image 29
ivan123 Avatar answered Oct 04 '22 05:10

ivan123