NSCollectionview didSelectItems从未调用过

时间:2017-07-31 03:48:36

标签: nscollectionview

我正在创建一个没有xib / storyboard的NSCollectionview,当触摸item时,collectionView:didSelectItemsAt:方法永远不会被调用。 z_setupView()在viewdidload()中调用

/// init collectionview

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,我怎样才能调用委托函数?

1 个答案:

答案 0 :(得分:5)

这是我的解决方案:默认情况下无法选择NSCollectionView项,我们必须设置

collectionview.isSelectable = true