Multiple Selection with TableView and CollectionView on iOS 13

Tram Ho

Introduce:

  • WWDC 2019 has introduced a new function for UItableViewCell. You can give users two fingers and swipe to select items faster on UITableView or UICollectionView.

Target:

  • Use MultiSelection to select item on TableView or CollectionView.
  • Implement this function with the sample project

Understand MultipleSelection Gestures:

  • In iOS 13, applications with this feature will help users to quickly select multiple items. For example, when TableView or CollectionView recognizes two fingers to touch, the application will automatically allow the user to swipe two fingers to select multiple items.

Start with the sample project:

Open Xcode and create new Xcode project. Select Single View App and click next.

Name the project and initialize.

Implementation:

  • First we remove the default ViewController in Main.storyboard and add a TableViewController and will add the NavigationController. Next we check the box Is Initial View Controller for NavigationController so that when starting the application will run this screen correctly.

  • Now we move to the ViewController.swift file to code. We have to make sure that ViewController has been inherited from UITableViewController.

Implementing Multiple Selections in TableView:

  • To use multiple selection on tableView, set true for the allowsMultipleSelectionDuringEditing property of tableView and add an edit button on navigationItem.

Add Delegate Method:

  1. In this delegate function we return true to allow the user to use the multiple selection function by swiping with two fingers.
  2. This delegate function will let the application know that the user started using two fingers to swipe and the edit function will be enabled.
  3. This delegate function will let the application know that the user has stopped swiping two fingers to select the item.

Build and Run:

  • For CollectionView also use the same delegate.

Reference: https://medium.com/better-programming/multiple-selection-gestures-on-tableview-and-collectionview-in-ios-13-77144506b742

Share the news now

Source : Viblo