let point = convert(event.locationInWindow, from: nil) for index in0..<numberOfItems(inSection: 0) { let frame = frameForItem(at: index) ifNSMouseInRect(point, frame, isFlipped) { clickedIndex = index break } }
returnsuper.menu(for: event) } }
let menu = NSMenu() menu.addItem(NSMenuItem(title: "Delete", action: #selector(didSelectDelete(_:)), keyEquivalent: "")) collectionView.menu = menu
@objcfuncdidSelectDelete(_ item: NSMenuItem) { guard let index = collectionView.clickedIndex, index < notes.count else { return } let indexPath = IndexPath(item: index, section: 0) notes.remove(at: index) collectionView.deleteItems(at: Set(arrayLiteral: indexPath)) }
For NSCollectionView with more than 1 sections
1
let frame = layoutAttributesForItem(at: IndexPath(item: index, section: 0))?.frame ?? .zero
Use Omnia
Omnia supports clicked indexPath for multi section NSCollectionView
1 2 3
collectionViewHandler.addMenuItem(title: "Add to Favorite", action: { item in print(item) })