i'm creating an NSCollectionview without xib/storyboard, when touched item, collectionView:didSelectItemsAt: method never called. z_setupView() 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)
}
}
}
// here is the 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)
}
}
without xib、sb,how can i call delegate functions ?
here is my solution: NSCollectionView item could not be selected on default,we must set
collectionview.isSelectable = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With