2

NSCollectionViewItem をもう一度クリックして選択を解除するにはどうすればよいですか?

これは、選択および選択解除に使用するコードです。

func collectionView(collectionView: NSCollectionView, didSelectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
        print("selected")
        guard let indexPath = indexPaths.first else {return}
        print("selected 2")
        guard let item = collectionView.itemAtIndexPath(indexPath) else {return}
        print("selected 3")
        (item as! CollectionViewItem).setHighlight(true)
    }

    func collectionView(collectionView: NSCollectionView, didDeselectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
        print("deselect")
        guard let indexPath = indexPaths.first else {return}
        print("deselect 2")
        guard let item = collectionView.itemAtIndexPath(indexPath) else {return}
        print("deselect 3")
        (item as! CollectionViewItem).setHighlight(false)
    }

/////////////////////

    class CollectionViewItem: NSCollectionViewItem {


        func setHighlight(selected: Bool) {

            print("high")
            view.layer?.borderWidth = selected ? 5.0 : 0.0
            view.layer?.backgroundColor = selected ? NSColor.redColor().CGColor : NSColor(calibratedRed: 204.0/255, green: 207.0/255, blue: 1, alpha: 1).CGColor
        }
    }

このコードは、別のアイテムがクリックされたときに delect を解除しますが、同じアイテムがクリックされたときは解除しません。同じ項目がクリックされたときに選択を解除したい。

4

3 に答える 3

0

簡単なトリックの 1 つは、CMD (マウスの左クリック) を使用することです。これで問題が正確に解決されるわけではありませんが、何もないよりはましです。

于 2016-08-02T10:02:35.740 に答える