I'm trying to work out how to amend insertion indexPaths in a collectionView for a peformBatchUpdates function using .map
Say I have items 1 2 3 4 5 6 7 in a collectionView and an array of indexPaths for deletion as
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 0
▿ 1 : 2 elements
- 0 : 0
- 1 : 1
and an array of indexPaths for insertion as
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 3
▿ 1 : 2 elements
- 0 : 0
- 1 : 4
I believe insertion indexPaths for a performBatchUpdates operation need to be calculated after the deletions have been performed so seeing the deletions are the first 2 items in the collectionView items, the indexPath.item for the insertionIndexPaths need to be decremented by 2 to have them insert in the correct position ie i want final insertionIndexPaths to be ...
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 1
▿ 1 : 2 elements
- 0 : 0
- 1 : 2
Essentially I think I need to check each indexPath.item in the insertionIndexPath array, see how many of the deletionIndexPaths were in front of that insertionIndexPath, and decrement the insertionIndexPath.item by that number.
How can i use .map on these insertionIndexPaths to get the correct result?
I've been able to do with
let adjustedPageDestinationIndexPaths = pageDestinationIndexPaths.map { ( indexPath ) -> IndexPath in
return IndexPath(item: indexPath.item - pageSourceIndexPaths.filter { $0.item < indexPath.item }.count , section: 0)
}
which gives correct adjusted indexPaths for the example below moving just 2 items from positions 0 and 1 to positions 3 and 4 but now realised that the greater the number of items moved at one time within the collectionView, the greater the number of items I need to track to calculate the source indexPath and destination indexPath adjustments on each loop. Back to the drawing board.