0

我正在使用集合视图,并且正在尝试设置 peek & pop 来使用它。我已经覆盖:

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?

我正在尝试使用 location 变量检索我单击的项目的索引路径。

位置似乎是正确的,但由于某种原因collectionView.indexPathForItem(at: location)返回了错误的索引路径。

我知道如何解决这个问题,但我不确定在 Swift 中是否可行。这是我的想法:

使用该函数indexPathForVisibleItems检索所有可用的 IndexPath。遍历这些 IndexPaths 并使用一个函数,locationForItem(at: indexPath)如果它存在的话,它会被调用。然后我可以根据它的位置找到正确的 IndexPath。

如果这是不可能的,我想知道是否有其他人有同样的问题,它在哪里返回完全错误的索引路径?我认为正在发生的事情是我单击的位置可能有多个单元格,它正在抓取视图层次结构中的顶部单元格,并返回该顶部单元格的 indexPath,这可能是这个 Z 中唯一的一个 -指数。

是否可以检索某个位置的所有索引路径?

代码:

override func viewDidLoad()
    if traitCollection.forceTouchCapability == .available {
        registerForPreviewing(with: self, sourceView: collectionView)
    }

override func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let indexPath = collectionView.indexPathForItem(at: location)...
4

0 回答 0