NSCollectionview didSelectItemsAt 从未调用过

NSCollectionview didSelectItemsAt never called

我正在创建一个没有 xib/storyboard 的 NSCollectionview,当触摸项目时,collectionView:didSelectItemsAt:方法从未被调用。 z_setupView() 在 vi​​ewdidload()

中调用

/// 初始化集合视图

fileprivate func z_setupView() {
    view.addSubview(topCollectionView)
    topCollectionView.delegate = self
    topCollectionView.dataSource = self
    let flLayout = NSCollectionViewFlowLayout()
    topCollectionView.collectionViewLayout = flLayout
    topCollectionView.register(BannerCollectionItem.self, forItemWithIdentifier: "BannerCollectionItem")

    topCollectionView.snp.remakeConstraints {[weak self] (make) in
        make.top.equalTo((self?.view.snp.top)!).offset(10)
        make.leading.equalTo(0)
        make.trailing.equalTo(0)
        make.height.equalTo(200)
    }
}    
}

// 这里是 NSCollectionViewItem

class BannerCollectionItem: NSCollectionViewItem {

 public var bannerModel: BannerAdModel? {
    didSet {
        adImageView.image = #imageLiteral(resourceName: "a_img_beijin")
        adImageView.frame = NSRect(x: 0, y: 0, width: 80, height: 80)

    }
}
fileprivate var adImageView = NSImageView()
override func viewDidLoad() {
    super.viewDidLoad()
}
override func touchesBegan(with event: NSEvent) {
    debugPrint("BannerCollectionItem touchesBegan")
}
override func loadView() {
    self.view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 200))
    self.view.addSubview(adImageView)

}  
}

没有xib、sb,如何调用委托函数?

我的解决方法是: NSCollectionView item不能默认选中,我们必须设置

collectionview.isSelectable = true