我创建一个NSCollectionview没有XIB /故事板,当触摸项目,collectionView:didSelectItemsAt:方法从未调用。 z_setupView()调用viewDidLoad中()NSCollectionview didSelectItemsAt从来没有叫
///初始化的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)
}
}
没有厦门国际银行,SB,我怎么能调用委托的功能呢?
这里是我的解决方案:NSCollectionView项目无法在默认选择,我们必须设置的CollectionView'isSelectable'是真实的topCollectionView.isSelectable = true –
我试着排列你的第一个代码示例,发现你有额外的花括号:) – Yana
你可以把你的答案作为答案,而不是作为评论;)@Geezher – Yana