TableView Editing & Deleting of rows in Swift

Todays topic is TableView Editing & Deleting of rows in Swift which Part 2 of our previous topic simple tableview.

In this we will cover how to handle row selection and deletion of row from table view.

I’ll suggest you to go through table view delegate documentation page for more functions. So we will use below methods.

  1. Handling row selection in tableview.



The above method is called when ever you click on the row of table view. Let us just display an alert whwnever user click on any table view row. 


Now build and run your project and click on different rows. you will see alert with diifferent row numbers.

 you can see that the colour of the row also changed after selection, you can modiify it according to your need.

  1. Handling row editing : In this we will use another two methods 

First is a bool method which enable row editing mode of the table view.

 when user swipe across the row it will show a “Delete” button on the right side of the selected row. 

Now swipe right to left tableview will look like this with Delete button on right.

Deleting row at indexpath from tableview in Swift

Second is handling the row editing in this we will delete the data from the mutable array, we need the mutable array not the array because it can handle the deletion and addition, after deleting data we will then reload the table for changes. 

Your method should look like this now.

       After deleting from data and reloading tableview will look like this.deleted row from table view swift

Tis the simplest form of deleting and selecting row you can modify these methods according to your requirements.

If there is any question do comment bellow.


Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.