2017-07-31 45 views
2

我創建一個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,我怎麼能調用委託的功能呢?

+1

這裏是我的解決方案:NSCollectionView項目無法在默認選擇,我們必須設置的CollectionView'isSelectable'是真實的topCollectionView.isSelectable = true –

+0

我試着排列你的第一個代碼示例,發現你有額外的花括號:) – Yana

+0

你可以把你的答案作爲答案,而不是作爲評論;)@Geezher – Yana

回答

3

這裏是我的解決方案:NSCollectionView項目不能在默認選中,我們必須設置

collectionview.isSelectable = true 
+0

你也可以接受答案;)。如果它解決了你的問題。 – Yana

+0

好的,但今天我不能接受這個答案('。。。。。) –