NSCollectionview didSelectItemsAt never called

I am creating an NSCollectionview without xib / storyboard, on touching item, the collectionView: didSelectItemsAt: method was never called. z_setupView () is called in 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 is located here

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)

}  
}

      

without xib, sb, how can I call the delegate functions?

+3


source to share


1 answer


here is my solution: NSCollectionView item cannot be selected by default, we have to set



collectionview.isSelectable = true

      

+5


source







All Articles