如何通過再次點擊取消選擇NSCollectionViewItem?通過點擊取消選擇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
}
}
此代碼deslect另一個項目被點擊時,而不是在相同的產品。我想在點擊同一項目時解散。