UITableViewCell Swipe Actions with Swift 

Hey Everyone,

In this article, I will share you information about the swipe property of cells in the UITableView. As you know, the UITableView is one of the most used UI elements. The swipe feature used in the cell structure of the UITableView is of great importance in terms of user interaction and user experience.

Adding swipe actions to UITableView rows has been possible since iOS 8 but only on the right side.With IOS 11 updates, the scrolling feature of each row element started to be used in two directions. In addition, the feature of adding images to the scrolling operation started to be used.

Apple use this feature in many parts such as mail, messages and reminders.

Step 1: Add UITableView

First of all, We add UITableView in ViewController and define datasource and delegate methods. (UITableViewDelegate , UITableViewDataSource)

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {

super.viewDidLoad()

self.tableView.dataSource = self

self.tableView.delegate = self }

extension ViewController : UITableViewDelegate , UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

<#code#>

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

<#code#>

}

}

Step 2: Create Cell

We add a new cell into the UITableView in the Storyboard, and only add a label to the Content section of the Cell. (You can add images, title, button, etc. depending on what you need.)

I created a list to fill in the information of each cell.

var names : [String] = [“Emircan Uzel” , “Elon Musk” , “Bill Gates” , “Jeff Bezos”]

I set the names in the list for each row to the text of the label of Cell. Now we have an application that lists only the names. Let’s add the swipe feature to the cells.

Step 3: Add TrailingSwipeActionsConfigurationForRowAt

We are adding the trailingSwipeActionsConfigurationForRowAt property of the UITableView. This will allow us to customize the swipe property and swipe operation for each cell.

@available(iOS 11.0, *)

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

let call = UIContextualAction(style: .normal, title: “Call”) { action, view, complete in

print(“call”)

complete(true)

}

call.image = UIImage(named: “call”)

call.backgroundColor = .green

let delete = UIContextualAction(style: .destructive, title: “Delete”) { action, view, complete in

print(“delete”)

complete(true)

}

delete.image = UIImage(named: “delete”)

delete.image?.withTintColor(.white)

delete.backgroundColor = .red

return UISwipeActionsConfiguration(actions: [delete,call])

}

In this section, I created two actions, call and delete. I assigned a title and image for each action.

I have installed the sample application of this article on my GitHub account. You can access my GitHub account from the section below. I hope it was a useful article for you. See you in the next article.

LinkedIn: https://www.linkedin.com/in/emircanuzel
Mail: emircanuzel95@gmail.com
GitHub: https://github.com/emircanuzel

IOS Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store