NSCollectionView deselect cell on click

In my project, I've used NSCollectionView with a custom cell item. I want only a single selection on collectionView.

In the collectionViewItem I've many textFields & buttons. I'm able to select a specific cell on click. But I'm not able to deselect again clicking on same cell.

Though I have used following code to deselect cell:


var clickGesture: NSClickGestureRecognizer!
    
    override var isSelected: Bool {
        didSet {
            if isSelected {
                mainBox.borderType = .lineBorder
                //shortContentBox.fillColor = NSColor(red: 72/255, green: 112/255, blue: 255/255, alpha: 0.05)
                mainBox.addSelectionShadow()
                addGestureRecognizer()
            } else {
                mainBox.borderType = .noBorder
                //shortContentBox.fillColor = .controlColor
                mainBox.removeShadow()
                removeGestureRecognizer()
            }
        }
    }
    
    func addGestureRecognizer() {
        clickGesture = NSClickGestureRecognizer(target: self, action: #selector(onItemClick(_:)))
        clickGesture.buttonMask = 0x1
        clickGesture.numberOfClicksRequired = 2
        view.addGestureRecognizer(clickGesture)
    }

    func removeGestureRecognizer() {
        view.removeGestureRecognizer(clickGesture)
        clickGesture = nil
    }

    @objc func onItemClick(_ sender: NSGestureRecognizer?) {
        collectionView?.deselectAll(nil)
    }


My problem is, now I'm not able to click on textField or button, after selecting the cell. While I'm able to click on textField when the cell is not selected.


Any suggestions? How do I achieve selection & deselection on click?

Replies

Probably, the click gesture on cell is detected when you tap button. Then you deselect the cell and remove gesture.


I would try to add a clickgesture on the button itself

Example here for UITableView, but should be very similar for NSCollection.

https://stackoverflow.com/questions/53699487/swift-clicked-button-inside-tableview-cell-does-not-call-the-did-select-row-at

In the collectionViewItem I've many textFields & buttons. I'm able to select a specific cell on click. But I'm not able to deselect again clicking on same cell.


You could also try:

- allow multiple selection

=> Then when tapping an already selected would deselect it

- when selecting a cell, deselectall all but the current one

Tried this

- allowed multiple selection

=> On tapping already selected cell doesn't deselect it.

Have you put print statements to print each time you tap on a cell ?


var selectionIndexPaths: Set<IndexPath>