In my OSX app I have a collection view which is a subclass of NSCollectionView
.
I'm all satisfied with how the things are except the contextual menu, which I can't figure out yet.
So what I want is:
I know how to do it for NSOutlineView
or NSTableView
, but not for collection view.
I can't figure out how to get the index of the item clicked.
Does anyone have any ideas on how I can implement this?
Any kind of help is highly appreciated!
Basically, all of our solutions are capable of addressing requirements, but I would like to make a supplement to swift3+, which I think is a complete solution.
/// 扩展NSCollectionView功能,增加常用委托
class ANCollectionView: NSCollectionView {
// 扩展委托方式
weak open var ANDelegate: ANCollectionViewDelegate?
override func menu(for event: NSEvent) -> NSMenu? {
var menu = super.menu(for: event);
let point = self.convert(event.locationInWindow, from: nil)
let indexPath = self.indexPathForItem(at: point);
if ANDelegate != nil{
menu = ANDelegate?.collectionView(self, menu: menu, at: indexPath);
}
return menu;
}
}
/// 扩展NSCollectionView的委托
protocol ANCollectionViewDelegate : NSObjectProtocol {
func collectionView(_ collectionView:NSCollectionView, menu:NSMenu?, at indexPath: IndexPath?) -> NSMenu?
}
This is what I wrote an extension, and I hope to help everyone.
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