Select multiple items using Pan Gesture

Updated 31 August 2021


In this article, we will discuss how to select multiple items using Pan Gesture.

Recently Apple has announced that we can select the multiple rows in table view using two fingers.

It would be easy and cool to select rows by simply select and drag. We would implement this using the Pan gesture. Selecting multiple items using Pan Gesture requires iOS 13 and above. Let’s move to the article.

How to select multiple items using Pan Gesture

Firstly, we need to set up our table view. Just create a table view assign delegate and dataSource to table view and implement the functions of the Table view protocol.

Select multiple items using Pan gesture

Adding functionality for multiple select using pan gesture

Now we have set up the basic configuration for select multiple items using Pan gesture. After that, we would require the data to populate the table view rows. We have created a model with variables var name and var isRowSelected.

For enabling multiple selections during editing we should set allowsMultipleSelectionDuringEditing property of the table view to true in viewDidLoad()

We have created an array that holds the dataModel values as var model = [dataModel]()

Now it’s time to assign the values to the array as:

Call the setDataIndataModel function in viewDidLoad()

Above all, we will write the table view protocol functions that are used to implement multiple selections. Here is the code:

Methods required for multiple select using two-fingers

I will start by listing the necessary methods of table view delegate to implement the multiple select using two-fingers. After that, I will explain the methods listed below.

shouldBeginMultipleSelectionInteractionAt method allows users to use two-finger select when set to true.

In didBeginMultipleSelectionInteractionAt method we need to set the setEditing property to true. In other words, we allow the table view to edit the rows.

tableViewDidEndMultipleSelectionInteraction method is called when the editing of the table view rows ends. In other words, when the user stops the two-finger pan gesture.

didSelectRowAt gives the value of the selected row. We are checking if the row is selected otherwise, we are toggling the value of the model array for the isRowSelected variable.

didDeselectRowAt method works opposite to didSelectRowAt. For instance, if the user deselects the row then we change the value of isRowSelected

In addition, click here for the Apple documentation.

We hope you would like my article.

. . .

Leave a Comment

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

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home