Try using collectionView(_ collectionView: UICollectionView,
targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath,
toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath
See Apple's docs: https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618052-collectionview
During the interactive moving of an item, the collection view calls
this method to see if you want to provide a different index path than
the proposed path. You might use this method to prevent the user from
dropping the item in an invalid location. For example, you might
prevent the user from dropping the item in a specific section.
So for example, if you wanted to prevent reordering a cell with the last cell you could do:
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
if proposedIndexPath.row == data.count {
return IndexPath(row: proposedIndexPath.row - 1, section: proposedIndexPath.section)
} else {
return proposedIndexPath
}
}