gpt4 book ai didi

NSCollectionview didSelectItemsAt 从未调用过

转载 作者:行者123 更新时间:2023-12-01 11:19:47 29 4
gpt4 key购买 nike

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

///初始化 Collection View

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,怎么调用委托(delegate)函数?

最佳答案

这是我的解决方案: NSCollectionView 项目默认不能被选中,我们必须设置

collectionview.isSelectable = true

关于NSCollectionview didSelectItemsAt 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407190/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com