代码之家  ›  专栏  ›  技术社区  ›  GeeZher å¼

从未调用NSCollectionview didSelectItemsAt

  •  3
  • GeeZher å¼  · 技术社区  · 7 年前

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

    ///初始化集合视图

    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)
    
    }  
    }
    

    没有XIBSB,我如何调用委托函数?

    1 回复  |  直到 7 年前
        1
  •  11
  •   GeeZher å¼    7 年前

    这是我的解决方案。无法在默认设置上选择nCollectionView项。我们必须设置

    collectionview.isSelectable = true