ios - UICollectionViewDropDelegate 错误的目标索引路径在 `dropSessionDidUpdate`

标签 ios swift uicollectionview drag-and-drop uikit

我试图在这个狭长的 Collection View 中实现拖放: UICollectionView

它具有水平布局,具有不同大小的单元格和部分。

拖动交互效果很好,但我注意到 UICollectionViewDropDelegate 中存在问题:

func collectionView(
    _ collectionView: UICollectionView,
    dropSessionDidUpdate session: UIDropSession,
    withDestinationIndexPath destinationIndexPath: IndexPath?)
    -> UICollectionViewDropProposal {

    if let destination = destinationIndexPath {
        print(destination) // Prints WRONG index path!!!
        return UICollectionViewDropProposal(
            operation: .move, intent: .insertAtDestinationIndexPath
        )
    }

    return UICollectionViewDropProposal(
        operation: .cancel, intent: .unspecified
    )
}

错误的目标索引路径传递给 collectionView(_:dropSessionDidUpdate:withDestinationIndexPath:)
因此,我无法正确确定该部分并确定该部分是否可用。

最佳答案

所以,这是 UIKit 的一个错误。正确的目标索引路径可以计算如下:

func collectionView(
    _ collectionView: UICollectionView,
    dropSessionDidUpdate session: UIDropSession,
    withDestinationIndexPath destinationIndexPath: IndexPath?)
    -> UICollectionViewDropProposal {

    // Calculating location in view
    let location = session.location(in: collectionView)
    var correctDestination: IndexPath?

    // Calculate index inside performUsingPresentationValues
    collectionView.performUsingPresentationValues {
        correctDestination = collectionView.indexPathForItem(at: location)
    }

    guard let destination = correctDestination else {
        return UICollectionViewDropProposal(
            operation: .cancel, intent: .unspecified
        )
    }

    // check destination 
    // ...
}

为了修复这个错误,首先,我尝试结合使用 location(in:)indexPathForItem(at:)。生成的索引路径等于委托(delegate)方法提供的 destinationIndexPath。为什么?我的注意力被 UIDataSourceTranslating 吸引了.它是一种协议(protocol),允许集合和表格 View 显示占位符单元格以进行拖放,而无需更改实际数据源。当拖放交互结束时,可以轻松删除占位符。所以,我做了一个假设

  1. destinationIndexPath 是在 indexPathForItem(at:)
  2. 的帮助下计算的
  3. 它忽略了由 UIDataSourceTranslating 创建的占位符,这是一个错误

然后我尝试将 indexPathForItem(at:) 包装到 performUsingPresentationValues(_:) 中,接收到的索引路径是正确的!

关于ios - UICollectionViewDropDelegate 错误的目标索引路径在 `dropSessionDidUpdate`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038184/

相关文章:

ios - 如何在 Swift 中填充 TableView ?

ios - UICollectionViewCell 中的 UIScrollView 不工作

ios - 我应该在 cellForItemAtIndexPath 中设置 cell.bound/cell.frame 吗?

ios - 来自带有延迟加载的远程 URL 的 MSSticker 图像(添加加载器)

ios - GeoFire 和 Firebase 查询

ios - UILabel 在圆形自定义 UIView 中不可见

ios - Swift 中的 parser.parse() 导致 EXC_BAD_ACCESS

swift - 在滚动期间点击时,UICollectionView 不再识别点击单元格?

iphone - 在单独的 iOS 线程上延迟执行

ios - 在 Swift 中设置 CIFilter