Rearrange collection view cells within it's section

I have used UICollectionView Cells for my app. It is populated by Arrays.

I have two CollectionView Sections. Section '0' & Section '1' has it's own cells populated with arrays.

There are multiple cells in section 1.

I want to enable user to rearrange cells within section 1. User should not be able to move cell from section 1 to section 0.

I am using following code but It is giving me errors...

I tried searching on the web but I didn't got the answer so I am posting it here for help...

Code Block
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let sourceSection = sourceIndexPath.section
let destSection = destinationIndexPath.section
if (sourceSection < destSection) {
let tempInputDataArray = inputDataArray.remove(at: sourceIndexPath.item)
inputDataArray.insert(tempInputDataArray, at: destinationIndexPath.item)
UserDefaults.standard.set(inputDataArray, forKey: "KeyForInputDataArray")
for element in inputDataArray {
print(element)
}
} else {
}
}

Unless I miss something, you want to authorise change only within the same section. So, you should change line 6 as:

Code Block
if (sourceSection == destSection) {

With your code, you could move from section 0 to section 1.

In the else { }, you could just emit a beep, to let user know something wrong occurred.

Or even better, create a counter of mismoves:
Code Block
var counter = 0

In else, increment counter and trigger alert only if count is 3

Code Block
} else {
count += 1
if count >= 3 {
// display alert
count = 0 // Then reset count to 0
} else {
// play a beep
}
}

If that solves the problem, don't forget to close the thread by marking correct answer.
Otherwise, please give details on errors:

I am using following code but It is giving me errors...


I used your suggestion of 'if (sourceSection == destSection)' but user is able to still drag from section 1 to 0...

Also used your code for else statement but it never gets called.

I think answer should be Collection View 'Section' Specific.

As per requirement user should be able to only move cells within sections i.e. Section 1 cells should be rearranged within section 1 only..
I was able to solve it myself...

Thanks Again...
Have you tried implementing collectionView(_:targetIndexPathForMoveFromItemAt:toProposedIndexPath:)?
collectionView(_:targetIndexPathForMoveFromItemAt:toProposedIndexPath:)
Code Block
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
if originalIndexPath.section == proposedIndexPath.section {
return proposedIndexPath //Accept moving to the proposedIndexPath
} else {
return originalIndexPath //Keep staying at the originalIndexPath
}
}


@elitedeveloper 

I was able to solve it myself...

It would be kind to explain how. And don't forget to close the thread.
@Claude31

I used solution similar to OOPer has given above and it solved the problem...
Yes, that's a clean way to do it with func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath

And gives equivalent result to what I proposed.

Note: for user experience, alerting (as I explained) why he cannot move would be useful.
Rearrange collection view cells within it's section
 
 
Q