Posts

Post marked as solved
12 Replies
No, it's a half solution. As I said in the end of the question, When I try to select a BlueItem: if I click on an empty space inside the BlueItem, the RedCollectionView delegate is triggered as expected, but if I click on one of the green parts inside the BlueItem, the RedCollectionView delegate isn't triggered.The 3 points at the end of the issue are still unsolved.
Post marked as solved
12 Replies
Hi, did you get a chance to look at the SO question ? I'm sorry, I know you're busy.
Post marked as solved
12 Replies
Ok, it's written. And yes you're right, precise and clear 😉https://stackoverflow.com/questions/56946775/nscollectionview-as-nscollectionviewitem
Post marked as solved
12 Replies
Is it Ok to continue the subject in stackoverflow.com ? It's easier to add images (screenshot) to explain my case.https://stackoverflow.com/questions/56946775/nscollectionview-as-nscollectionviewitem
Post marked as solved
12 Replies
Yes, you’re right. So I’ve subclassed the ChildCollectionView, especially mouseDown: and mouseUp: I’ve set the ChildCollectionView delegate to the ParentCollectionView delegate. So now in collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) method, if the collection view is the parent, so normal selection is performed. Otherwise, I get the center point of the child collection view, and ask the parent collection for the corresponding indexPath. The fact that the child collection layout displays only on the first row isn’t new. It always did that. If I add a NSClipView, or if I change the flow layout by a grid layout, it displays well, but selecting won’t work.
Post marked as solved
12 Replies
So I've finally found a solution.In the ParentItem: class ChildCollectionView: NSCollectionView { override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) self.nextResponder?.mouseDown(with: event) } override func mouseUp(with event: NSEvent) { super.mouseUp(with: event) self.nextResponder?.mouseUp(with: event) } } lazy var mycollectionView: ChildCollectionView = { let mycollectionView: ChildCollectionView = ChildCollectionView.init(frame: NSRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) mycollectionView.collectionViewLayout = self.collectionViewLayout mycollectionView.dataSource = self mycollectionView.isSelectable = true mycollectionView.backgroundColors = [NSColor.blue, NSColor.blue] return mycollectionView }()In the ParentViewController: func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let item: NSCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "ParentItemID"), for: indexPath) guard let parentItem: ParentItem = item as? ParentItem else { return item } if parentItem.mycollectionView.delegate !== self { parentItem.mycollectionView.delegate = self } return parentItem } func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { if collectionView === self.collectionView { indexPaths.forEach({ print($0) }) } else { let center: NSPoint = collectionView.center if let indexPath = self.collectionView.indexPathForItem(at: center) { print(indexPath) } } collectionView.deselectItems(at: indexPaths) }And a small extension in order to get the center point:extension NSView { var center: NSPoint { return CGPoint(x: NSMidX(self.frame), y: NSMidY(self.frame)) } }BUT, I have an issue in the ParentItem, with the flow layout: It continues on a single row. It's like it doesn't know the collectionview size, in order to change rows.Now if I use NSCollectionViewGridLayout, it displays well, but selection doesn't work anymore.