UITableView Leading & Trailing Swipe Actions in iOS 11 🎷

Written by p.gpt10 | Published 2017/11/02
Tech Story Tags: ios | ios-11 | swift | objective-c | uitableview

TLDRvia the TL;DR App

Everyone needs a good editor..!!!✂️

Since iOS 8, Apple has provided us with the provision to add our own customized actions to a U_ITableView’s_ rows using the U_ITableViewDelegate_ method — t_ableView(_:editActionsForRowAt:)._

tableView(_:editActionsForRowAt:)

Asks the delegate for the actions to display in response to a swipe in the specified row.

Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.

Now that iOS 11 is here, we have some new additions 🤓 to the table view’s editing API. Let’s see what’s in it for us.👀

What’s new in iOS 11?

With the release of iOS 11, Apple introduced two new methods 😮 for handling Swipe Actions in UITableView to support swipe actions on both leading as well as trailing edge of the row, i.e.

  1. tableView(_:leadingSwipeActionsConfigurationForRowAt:) — swipe actions to display on the leading edge of the row
  2. tableView(_:trailingSwipeActionsConfigurationForRowAt:) — swipe actions to display on the trailing edge of the row

These methods were introduced as a part of UITableViewDelegate.

Swipe Actions — Optional

The methods to handle swipe actions in iOS 11 are optional 😅, i.e.

  1. if implemented, supersede the tableView(_:editActionsForRowAt:) delegate method.
  2. if not implemented, tableView(_:editActionsForRowAt:) will be called as the default behaviour.

Swipe Actions — Full Swipe

In iOS 11, swipe actions appear as Full Swipe by default 🤔, i.e the swipe spans the complete width of the UITableView.

The Full Swipe behaviour can be configured using performsFirstActionWithFullSwipe property 👏, introduced as a part of UISwipeActionsConfiguration — The set of actions to perform when swiping on rows of a table.

When this property is set to true, a full swipe in the row performs the first action listed in the actions property. The default value of this property is true.

Promotions

Don’t forget to read my other articles:

  1. Everything you’ve always wanted to know about notifications in iOS
  2. Drag It & Drop It in Collection & Table — iOS 11
  3. Everything about Codable in Swift 4
  4. Color it with GRADIENTS — iOS
  5. All you need to know about Today Extensions (Widget) in iOS 10
  6. UICollectionViewCell selection made easy..!!

Feel free to leave comments if you have any doubts. 🙂🙃


Published by HackerNoon on 2017/11/02