Enable slide to delete in UITableView.

I’m sure you’ve done it hundreds of times, whether it be sliding a message or sliding an email.

In iOS a lot of UITableViews have something that looks like this:

Good News!

To achieve this, it’s super simple to do.

It’s actually ridiculously easy.

I won’t be going over how to set up a UITableView though you need to have a working UITableView to achieve this.

If you would like, you can go over to my GitHub and download my project example I created for Fetch JSON using NSURLSession and populate a UITableView Part 1.

Hopefully by now you have a working UITableView with some data displaying.

Implementation.

This only takes a few lines so you can stop stressing out.

Add this func:

Inside this function we need to:

  • Check what type of edit was committed.
  • Remove the object from the array
  • Delete the row with an animation

We can do this like so:

Another way to implement.

This way is a lot better, mainly in my opinion due to no if else statements.

Because in the other method, if you were to have more options then you would need to implement if else statements to handle the actions.

Also you noticed I’ve added another option when sliding, a share button.

Done!

Yup, thats it! Pretty easy right?

I hope this article helped out so now you can go ahead and easily implement this into your applications.

Like always hope to get feedback.

Comments & suggestions are very much appreciated.

Let me know what to write about next!

Thank you.