【问题标题】:NSCollectionview didSelectItemsAt never calledNSCollectionview didSelectItemsAt 从未调用过
【发布时间】:2017-07-31 03:48:36
【问题描述】:

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

/// 初始化集合视图

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

【问题讨论】:

  • 这里是我的解决方案: NSCollectionView 项目默认不能被选中,我们必须设置collectionview isSelectable 为true topCollectionView.isSelectable = true
  • 我试图排列你的第一个代码示例,发现你有额外的右花括号:)
  • 您可以将您的答案作为答案,而不是作为评论;)@Geezher
  • 感谢您的回复,大括号可能是'comment format' ⚆_⚆

标签: nscollectionview


【解决方案1】:

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

collectionview.isSelectable = true

【讨论】:

  • 您也可以接受答案;)。如果它解决了您的问题。
猜你喜欢
  • 2021-09-21
  • 2019-01-17
  • 1970-01-01
  • 2014-11-23
  • 2017-08-27
  • 2012-08-09
  • 2013-09-04
  • 2011-05-13
相关资源
最近更新 更多