collectionView(_:didDeselectItemsAt :)从未被调用 [英] collectionView(_:didDeselectItemsAt:) is never called

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

问题描述

因为某些原因,我的 NSCollectionView (使用我写的自定义布局)调用其委托的 collectionView(_:didSelectItemsAt:)函数当我点击一个项目,但不是相同的代理的 collectionView(_:didDeselectItemsAt:)当我点击它。



这是我的基本设置:

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

扩展MyCollectionViewContainer:NSCollectionViewDelegate {

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

print(Selected,indexPaths)

//突变数据以反映该选择


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

未选择从未打印...:/

解决方案

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


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.

Here's my basic setup:

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... :/

解决方案

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天全站免登陆