我有一個自定義視圖,其中包含一個如下所示的集合視圖。collectionView didSelectItemAt indexPath未調用swift
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: scaled(height: 15), left: scaled(width: 35), bottom: scaled(height: 15), right: scaled(width: 35))
layout.itemSize = CGSize(width: scaled(width: 30), height: scaled(width: 30))
layout.minimumLineSpacing = 15
layout.minimumInteritemSpacing = 30
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(THTexasHoldemEmojiCell.self, forCellWithReuseIdentifier: THTexasHoldemEmojiCell.className)
}
和委託職能
extension THTexasHoldemEmojisView {
func setupDelegates() {
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension THTexasHoldemEmojisView: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
print("did highlight item")
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("did select item")
}
}
奇怪的是didHighlightItem函數可以被調用,但didSelectItem不會。我在這裏錯過了什麼嗎?謝謝你的幫助。
我的視圖連接是UIViewController(THController)包含UIView(THEmojisView),THEmojisView包含集合視圖。 在THController中,我有很多觀點和操作,但不包括THEmojisView。 THController的touchesBegan(_ touches:Set,with event:UIEvent?)是否會影響集合視圖的委託funcs?
請在下次發佈代碼而不是截圖! – Rikh
您是否設置了數據源和委託? – iDeveloper
'yourCollectionView?.delegate = self'和 'yourCollectionView?.dataSource = self' – iDeveloper