![](/img/trans.png)
[英]didSelectItemAtIndexPath Doesn't Work In Collection View Swift
[英](Swift) didSelectItemAtIndexPath in UICollectionView defined in external view doesn't work
這是我的問題。 我做了一個繼承自UIViewController,UICollectionViewDataSource和UICollectionViewDelegate的View Controller。 所以,我有這個:
class myController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
...
}
然后,我制作了一個自定義視圖,它具有一個UICollectionView,如下所示:
class externalCollectionView: UIView {
var myCollection: UICollectionView?
override init(frame: CGRect){
super.init(frame: frame)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
layout.itemSize = CGSize(width: 130, height: 130)
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 10
myCollection = UICollectionView(frame: CGRectMake(0, 120, frame.size.width, frame.size.height - 120), collectionViewLayout: layout)
myCollection!.registerNib(UINib(nibName: "customCellView", bundle: nil), forCellWithReuseIdentifier: "myItem")
self.addSubview(myCollection!)
}
}
因此,在我的視圖控制器中,我在viewDidLoad方法中使用以下代碼設置了委托方法和功能:
customView = externalCollectionView(frame: self.view.frame)
customView.myCollection!.dataSource = self
customView.myCollection!.delegate = self
customView.myCollection!.userInteractionEnabled = true
customView.myCollection!.allowsMultipleSelection = true
這樣,永遠不會調用方法didSelectItemAtIndexPath。 是的,我有一個自定義單元格收集視圖(在xib中),但我什至沒有圖形元素(為空),並且未調用該方法。 我已經實現了幾種在Internet上找到的解決方案,但是沒有任何效果。 有人知道會發生什么嗎?
您是否嘗試過從Storyboard或Xib將UICollectionViewCell連接到文件的所有者委托和dataSource? 也許這可以幫助
您只需將自定義視圖的userInteractionEnabled
設置為true
。 如果父視圖未啟用用戶交互,則其子視圖將無法識別任何觸摸事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.