ОК, я нашел решение!
Я должен был использовать «contextMenuConfigurationForItemAt» вместо «configurationForMenuAtLocation».
вот так:
@available(iOS 13.0, *)
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
return self.makeContextMenu(for: indexPath.row)
})
}
Затем используйте вот этот:
@available(iOS 13.0, *)
func makeContextMenu(for index:Int) -> UIMenu {
var actions = [UIAction]()
for item in self.contextMenuItems {
let action = UIAction(title: item.title, image: item.image, identifier: nil, discoverabilityTitle: nil) { _ in
self.didSelectContextMenu(menuIndex: item.index, cellIndex: index) // Here I have both cell index & context menu item index
}
actions.append(action)
}
let cancel = UIAction(title: "Cancel", attributes: .destructive) { _ in}
actions.append(cancel)
return UIMenu(title: "", children: actions)
}
Вот мои пункты контекстного меню:
let contextMenuItems = [
ContextMenuItem(title: "Edit", image: IMAGE, index: 0),
ContextMenuItem(title: "Remove", image: IMAGE, index: 1),
ContextMenuItem(title: "Promote", image: IMAGE, index: 2)
]
А вот мой ContextMenuItem:
struct ContextMenuItem {
var title = ""
var image = UIImage()
var index = 0
}