ios – How you can Replace Array Index After Drag and Drop is Known as in UserDefaults in CollectionView?

[ad_1]

I am Creating an demo, On this I’ve Drag and Drop Performance , Drag and Drop perform is working Completely Advantageous, My Concern is , once I shut the app and got here again to the View once more, the identical is Index is displaying within the array, I need to Get the array as I do Drag and Drop at a index.

Here is My Code

CollectionView Strategies

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
    return array.rely
    
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
    cell.lbl.textual content = array[indexPath.row]
    cell.img.layer.cornerRadius = cell.img.body.measurement.width / 2
    cell.lbl.layer.cornerRadius = cell.lbl.body.measurement.width / 2
    return cell
}

func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: (collectionView.body.measurement.width / 2) - 20, peak: 200)
    }

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

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    let merchandise = array.take away(at: sourceIndexPath.merchandise)
    defaults.removeObject(forKey: "information")
    array.insert(merchandise, at: destinationIndexPath.merchandise)
    defaults.set(merchandise, forKey: "information")
    defaults.synchronize()
    
    print(merchandise )
}

View Did Load and LongPressGesture Strategies

 @IBOutlet weak var collectionView: UICollectionView!
 var array = ["Fateh", "Parul", "Jitendra", "Javed", "Brijesh","Pandey Ji"]
 let defaults = UserDefaults.customary
 var longPress : UILongPressGestureRecognizer!


override func viewDidLoad() {
    tremendous.viewDidLoad()
    // Do any extra setup after loading the view.
    if UserDefaults.customary.array(forKey: "information") == nil {
         // userDefault has a price
   
        defaults.set(array, forKey: "information")
        
    } else  {
        let newData = defaults.object(forKey: "information")
        array = newData as! [String]
    }
    
    
    collectionView.delegate = self
    collectionView.dataSource = self
    longPress = UILongPressGestureRecognizer(goal: self, motion: #selector(handleLongGesture(gesture:)))
    collectionView.addGestureRecognizer(longPress)
}

 @objc func handleLongGesture(gesture : UILongPressGestureRecognizer){
    swap gesture.state {
    case .started:
        guard let selectedIndex = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
            break
        }
        collectionView.beginInteractiveMovementForItem(at: selectedIndex)
    case .modified:
        collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view))
        
    case .ended:
        collectionView.endInteractiveMovement()
        defaults.synchronize()
    @unknown default:
        collectionView.cancelInteractiveMovement()
    }
}

[ad_2]

Leave a Reply