On iOS 13 we have the beautiful context menu for tableView & collectionView.
I'm using it on UICollectionView like this:
Implementation on 'cellForItemAt indexPath':
let interaction = UIContextMenuInteraction(delegate: self)
cell.moreButton.isUserInteractionEnabled = false
cell.moreButton.tag = indexPath.row
cell.addInteraction(interaction)
Handle the tiger on 'configurationForMenuAtLocation location'
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in
var actions = [UIAction]()
for item in self.contextMenuItems {
let action = UIAction(title: item.title, image: item.image, identifier: nil, discoverabilityTitle: nil) { _ in
self.didSelectContextMenu(index: 0) <== how pass the index from here?
}
actions.append(action)
}
let cancel = UIAction(title: "Cancel", attributes: .destructive) { _ in}
actions.append(cancel)
return UIMenu(title: "", children: actions)
}
return configuration
}
The question is how should I know which index of collectionView is triggered this menu?
OK I found the solution!
I should've use the 'contextMenuConfigurationForItemAt' instead of 'configurationForMenuAtLocation'.
like this:
@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)
})
}
Then use this one:
@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)
}
Here are my context menu items:
let contextMenuItems = [
ContextMenuItem(title: "Edit", image: IMAGE, index: 0),
ContextMenuItem(title: "Remove", image: IMAGE, index: 1),
ContextMenuItem(title: "Promote", image: IMAGE, index: 2)
]
And here is my ContextMenuItem:
struct ContextMenuItem {
var title = ""
var image = UIImage()
var index = 0
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With