Swift indexPath 数组计算

标签 swift functional-programming uicollectionview nsindexpath performbatchupdates

我正在尝试找出如何使用 .map 修改 peformBatchUpdates 函数的 collectionView 中的插入索引路径

假设我有一个 collectionView 中的项目 1 2 3 4 5 6 7 和一个要删除的 indexPaths 数组

▿ 2 elements
  ▿ 0 : 2 elements
    - 0 : 0
    - 1 : 0
  ▿ 1 : 2 elements
    - 0 : 0
    - 1 : 1

和一个用于插入的索引路径数组

▿ 2 elements
  ▿ 0 : 2 elements
    - 0 : 0
    - 1 : 3
  ▿ 1 : 2 elements
    - 0 : 0
    - 1 : 4

我相信需要在执行删除后计算performBatchUpdates操作的插入indexPaths,因此看到删除的是collectionView项中的前2项,insertionIndexPaths的indexPath.item需要减2才能有它们插入到正确的位置,即我希望最终的 insertIndexPaths 是...

▿ 2 elements
  ▿ 0 : 2 elements
    - 0 : 0
    - 1 : 1
  ▿ 1 : 2 elements
    - 0 : 0
    - 1 : 2

本质上,我认为我需要检查insertionIndexPath数组中的每个indexPath.item,查看该insertionIndexPath前面有多少deletionIndexPaths,并将insertionIndexPath.item减少该数量。

如何在这些插入索引路径上使用 .map 以获得正确的结果?

最佳答案

我已经能够做到

            let adjustedPageDestinationIndexPaths = pageDestinationIndexPaths.map { ( indexPath ) -> IndexPath in
                return IndexPath(item: indexPath.item - pageSourceIndexPaths.filter { $0.item < indexPath.item }.count , section: 0)
            }

它为下面的示例提供了正确的调整后的索引路径,仅将 2 个项目从位置 0 和 1 移动到位置 3 和 4,但现在意识到在 collectionView 中一次移动的项目数量越多,项目数量就越多我需要跟踪计算每个循环上的源索引路径和目标索引路径调整。回到绘图板。

关于Swift indexPath 数组计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127212/

相关文章:

ios - UICollectionView insertItemAtIndexPath 不工作

ios - 如何快速解析 Json 文件以获取 2 个 Collection View ?

swift - 如何使手势识别器选择器在 swift 3 中工作?

haskell - fmap和bind之间的能力差异?

function - F# 中的函数值是如何传递的?

scala - free monad 应该基于的仿函数在哪里

ios - UIcollectionviewcell里面的UIcollectionview以及如何显示数据

ios - 除非通过 xcode 启动,否则应用程序在加载屏幕后崩溃

iOS CLLocationManager : Removing spikes

objective-c - 调试控制台不显示 Swift + Objective-C 的值