Luke
Luke

Reputation: 9700

Prevent UICollectionView interactive reordering from reordering to certain index paths

I've built a collection view which allows me to drag and re-order elements. Certain elements (in my case, the first three and the last two), should not be re-orderable, i.e. element 0 must always remain at position 0 in my model array, etc.

override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool

This function is aware of this and will return false for those elements. I can't drag them. However, I can drag other elements into their positions, the default collection view reordering UI allows for this.

What I want to do is only allow dragging between elements which can be moved. Not sure how I can configure the collection view to stop me picking up and dragging certain cells.

Upvotes: 0

Views: 520

Answers (1)

Luke
Luke

Reputation: 9700

Turns out I needed this guy:

override func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath

Inside, I can check whether you're planning to move into those banned indexes and return the place I want you to go instead.

Upvotes: 1

Related Questions