Search code examples
iosswiftuicollectionviewdrag-and-dropios11

How do you make it so you can't drag a specific cell in a UICollectionView in Swift 3 iOS 11 and above?


I have a collection view with a few spaces. The spaces can be filled with profile photos. I made the empty spaces have a button that triggers adding a photo. I made it so the uploaded photos can be dragged and dropped by making the view controller that manages the collection view conform to the UICollectionViewDragDelegate and UICollectionViewDropDelegate protocols. How do I make it so the empty spaces cannot be dragged?


Solution

  • In the collectionView itemsForBeginning method of the UICollectionViewDragDelegate, write logic to drag the cells you want to drag and then end with

    else {
       return [UIDragItem]()
     }
    

    and then cells that fall to the else will not be draggable.

    reference for my answer: https://stackoverflow.com/a/52183966/8441431

    (^^^They were answering a different question)