collectionView(_:didDeselectItemsAt:) 永远不会被调用 [英] collectionView(_:didDeselectItemsAt:) is never called

查看:27
本文介绍了collectionView(_:didDeselectItemsAt:) 永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我单击一个项目时,我的 NSCollectionView(使用我编写的自定义布局)调用其委托的 collectionView(_:didSelectItemsAt:) 函数,但当我点击它时,不是同一个委托的 collectionView(_:didDeselectItemsAt:).

For some reason, my NSCollectionView (which uses a custom layout that I wrote) calls its delegate's collectionView(_:didSelectItemsAt:) function when I click an item, but not that same delegate's collectionView(_:didDeselectItemsAt:) when I click off it.

这是我的基本设置:

class MyCollectionViewContainer: NSViewController {
    fileprivate lazy var collectionView: NSCollectionView = {
        let collectionView = NSCollectionView()
        collectionView.delegate = self
        collectionView.dataSource = self

        collectionView.collectionViewLayout = self.customLayout
        collectionView.isSelectable = true
        collectionView.allowsEmptySelection = true
        collectionView.allowsMultipleSelection = false
    }()
}

extension MyCollectionViewContainer: NSCollectionViewDelegate {

    func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {

        print("Selected", indexPaths)

        // Mutate data to reflect that selection
    }

    func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
        print("Deselected", indexPaths)
    }
}

取消选择"永远不会打印...:/

"Deselected" is never printed... :/

推荐答案

所以结果证明我的布局没有添加任何 NSCollectionElementKindInterItemGapIndicator;显然,这是集合视图用于检测何时单击项目的视图.我的快速解决方案是在我的布局中添加一个巨大的跨越整个集合视图的布局,并使其 Z 索引低于所有其他项目和补充视图的 Z 索引.

So it turns out my layout wasn't adding any supplementary views of type NSCollectionElementKindInterItemGapIndicator; apparently this is the view that the collection view uses to detect when you click off an item. My quick solution was to add one huge one to my layout that spans the entire collection view, and make its Z index below that of all other items and supplementary views.

这篇关于collectionView(_:didDeselectItemsAt:) 永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆