我正在尝试使用.map计算如何修改collectionView中的插入indexPaths以获取peformBatchUpdates函数
假设我在集合视图中有项目1 2 3 4 5 6 7,并且删除了一个indexPaths数组
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 0
▿ 1 : 2 elements
- 0 : 0
- 1 : 1
和一个indexPaths数组,用于插入
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 3
▿ 1 : 2 elements
- 0 : 0
- 1 : 4
我相信在执行删除之后需要计算performBatchUpdates操作的插入indexPath,因此看到删除是collectionView项中的前2项,insertIndexPaths的indexPath.item需要递减2才能将它们插入正确的位置,即我想最终的insertIndexPaths是...
▿ 2 elements
▿ 0 : 2 elements
- 0 : 0
- 1 : 1
▿ 1 : 2 elements
- 0 : 0
- 1 : 2
基本上我认为我需要检查insertionIndexPath数组中的每个indexPath.item,查看该insertIndexPath前面有多少个deletionIndexPaths,并将该数字减去insertIndexPath.item。
如何在这些insertIndexPaths上使用.map来获得正确的结果?
我已经能够做到了
let adjustedPageDestinationIndexPaths = pageDestinationIndexPaths.map { ( indexPath ) -> IndexPath in
return IndexPath(item: indexPath.item - pageSourceIndexPaths.filter { $0.item < indexPath.item }.count , section: 0)
}
为下面的示例提供了正确调整的indexPaths,仅将2个项目从位置0和1移动到位置3和4,但现在意识到在collectionView中一次移动的项目数越多,我需要的项目数量就越多跟踪以计算每个循环上的源indexPath和目标indexPath调整。回到绘图板。