Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which UICollectionView cell is triggered for context menu on (iOS 13+) configurationForMenuAtLocation

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?

like image 499
Ahmadreza Avatar asked Apr 11 '20 14:04

Ahmadreza


1 Answers

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
}
like image 81
Ahmadreza Avatar answered Oct 04 '22 04:10

Ahmadreza