2

我正在尝试使用实例方法. 两种解决方案都可以正常工作,但是当我尝试在 UICollectionView 中实现它们时,上下文菜单优先,我无法使用拖放重新排序。moveItemAtcontextMenuConfigurationForItemAt

有没有办法在同一个集合视图中使用这两种方法?或者,是否有人熟悉使用其他解决方案支持这些功能的方法?我目前正在考虑实施拖放委托作为一种解决方法。

下面是我正在使用的代码示例。该项目针对 iOS14。

extension myViewController {
    override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let temp = items.remove(at: sourceIndexPath.item)
        items.insert(temp, at: destinationIndexPath.item)
    }

    override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        let index = indexPath.row
        
        let config = UIContextMenuConfiguration(
            identifier: nil,
            previewProvider: nil){ [self] _ in
            return UIMenu(
                title: "",
                image: nil,
                identifier: nil,
                children: [deleteAction(index: index),editAction(index: index)])
        }
        return config
    }
}
4

1 回答 1

0

将您对 moveItemAt API 的使用替换为新的拖放 API,您将免费获得此行为!

于 2021-01-06T10:28:28.660 回答