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
        }
    }

此代码在单击另一个项目时取消,但在单击同一项目时不取消。单击同一项目时,我想取消选择。

4

3 回答 3

5

您可以通过观察项目上的选定状态,并NSClickGestureRecognizer在项目被选中时在项目的视图上安装一个,并在取消选择时卸载它来实现这一点。

将以下代码放在您的NSCollectionViewItem子类中的某处:

- (void)onClick:(NSGestureRecognizer *)sender {
    if (self.selected) {
        //here you can deselect this specific item, this just deselects all
        [self.collectionView deselectAll:nil];
    }
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        [self installGestureRecognizer];
    }
    else {
        [self uninstallGestureRecognizer];
    }
}

- (void)installGestureRecognizer {
    [self uninstallGestureRecognizer];

    self.clickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(onClick:)];
    [self.view addGestureRecognizer:self.clickGestureRecognizer];
}

- (void)uninstallGestureRecognizer {
    [self.view removeGestureRecognizer:self.clickGestureRecognizer];
    self.clickGestureRecognizer = nil;
}
于 2018-01-31T09:56:45.237 回答
0

一个简单的技巧是使用 CMD - 鼠标左键。虽然这并不能完全解决我的问题,但总比没有好。

于 2016-08-02T10:02:35.740 回答
0

感谢Marko Hlebar回答

基于此,我实现了点击item切换其选择的功能。

class MyItem: NSCollectionViewItem {
    override var isSelected: Bool {
        didSet {
            self.view.layer?.backgroundColor = (isSelected ? NSColor.blue : NSColor.gray).cgColor
            // If the selection causes the size to change, re-layout.
            self.collectionView?.collectionViewLayout?.invalidateLayout()
        }
    }

    override func viewDidLoad() {
        self.view.wantsLayer = true
        self.view.addGestureRecognizer(NSClickGestureRecognizer(target: self, action: #selector(onClick(sender:))))
    }

    @objc private func onClick(sender: NSGestureRecognizer) {
        guard let indexPath = self.collectionView?.indexPath(for: self) else { return }
        if self.isSelected {
            self.collectionView?.deselectItems(at: [indexPath])
        } else {
            self.collectionView?.selectItems(at: [indexPath], scrollPosition: [])
        }
    }
}
于 2021-09-24T22:48:40.217 回答