ios – drag & drop in collection view and updating indexpath in label


I have issue where i am using two collwctionview, one has data with id, photo and in second collection view I’m appending cell from first collectionview and performing drag and drop. in second collection view i have one label which is showing number of appending data in count as i am taking indexpath.count, now the issue is whenever i’m dragging and dropping cell i am getting repetation of indexpath in label. what should i do?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == stretchAddedCV{
return addRoutine.count
}
else{
return self.arrRoutine.count
}
}

// drag and Drope Mothed
func collectionView(_ UICollectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
    return true
}

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    guard destinationIndexPath.item < addRoutine.count else {
        return
    }
    let temp = self.addRoutine.remove(at: sourceIndexPath.item)
    self.addRoutine.insert(temp, at: destinationIndexPath.item)
    
    self.stretchAddedCV.reloadItems(at: [destinationIndexPath, sourceIndexPath])       
}

//drag and Drope Longe press
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
    switch gesture.state {
    case .began:
        guard let selectedIndexPath = self.stretchAddedCV.indexPathForItem(at: gesture.location(in: self.stretchAddedCV)) else {
            break
        }
        self.stretchAddedCV.beginInteractiveMovementForItem(at: selectedIndexPath)
    case .changed:
        self.stretchAddedCV.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
    case .ended:
        self.stretchAddedCV.endInteractiveMovement()
    default:
        self.stretchAddedCV.cancelInteractiveMovement()
    }}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img