paint-brush
[SWIFT] Reorder cells in UICollectionView using drag & dropby@dejanatanasov
13,908 reads
13,908 reads

[SWIFT] Reorder cells in UICollectionView using drag & drop

by Dejan AtanasovAugust 1st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this tutorial, I will cover how to reorder cells in <a href="https://hackernoon.com/tagged/uicollectionview" target="_blank">UICollectionView</a> using drag &amp; drop. I have seen many different approaches on how to do this, some are working decent and some are not. That's why I will show you the approach that is tested and working 100%.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - [SWIFT] Reorder cells in UICollectionView using drag & drop
Dejan Atanasov HackerNoon profile picture

In this tutorial, I will cover how to reorder cells in UICollectionView using drag & drop. I have seen many different approaches on how to do this, some are working decent and some are not. That's why I will show you the approach that is tested and working 100%.

To make this tutorial shorter, I will only present how to do the reordering and won't show all the required delegate methods. Since you got here, I count that you already know the UICollectionView basics.

UICollectionViewDelegate

We will start by using two delegate methods from the UICollectionViewDelegate protocol.

1 Enable the use of moving items in the UICollectionView by calling canMoveItemAt delegate method. Passing true will enable this feature.



func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {return true}

2 Next, we will implement the moveItemAt delegate method where you will intercept the starting index and the ending index of the both items that are switching places.




func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {print(“Starting Index: \(sourceIndexPath.item)”)print(“Ending Index: \(destinationIndexPath.item)”)}

UILongPressGestureRecognizer

For a better control of the gestures in the UICollectionView we will implement a UILongPressGestureRecognizer.

Note that these interactive functions work for iOS 9 and above.

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀

Check out my latest project:


‎VIP Sports Bet Tips & Scores on the App Store_This app is only available on the App Store for iOS devices. Increase your profits by following our professional BET…_apple.co

Read more of my writing on Medium:


Introducing Clean Swift Architecture (VIP)_Forget MVC, now!_hackernoon.com


Your ultimate guide to the Google Maps SDK on iOS, using Swift 4_Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…_medium.freecodecamp.org


SWIFT — Custom UIView with XIB file_Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…_medium.com


How to add Spotlight support to your iOS app_A Swift tutorial that will make your app available in Spotlight search_hackernoon.com


Core Data Relationships_Understanding One-to-One and One-To-Many relationships_hackernoon.com


Understanding Auto Layout in Xcode 9_All you need to know about Auto Layout_hackernoon.com

Subscribe to my Newsletter: