Byo*_*oth 8 ios uicollectionview swift
我很困惑,因为这只是一件简单的事情,但不起作用。
这是我在这里写问题之前的检查清单。
UICollectionViewDelegate 已注册
除了“调试视图层次结构”中的 UICollectionView 之外,没有任何内容
isUserInteractionEnabled UICollectionView 和 UICollectionViewCell 是 true
isAllowsSelection UICollectionView 是 true
还有什么我应该检查的吗?
======
class MyView: NibDesignable { // It's UIView with NibDesignableProtocol (https://github.com/mbogh/NibDesignable)
@IBOutlet weak var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
private func setup() {
print("test - It's called.")
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.isUserInteractionEnabled = true
self.collectionView.isAllowsSelection = true
}
}
extension MyView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath {
print("test - It's not called")
}
}
Run Code Online (Sandbox Code Playgroud)
我只粘贴了与问题相关的部分,因为所有代码都太长了。
如果您的自定义单元格中有一个gestureRecognizer,就会发生这种情况。这将覆盖 collectionView 的 didSelectItemAt 委托。
如果这是您的问题,那么您可以通过识别gestureRecognizer 并执行以下操作来解决它:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.someViewInMyCellTapped(_:)))
someViewInMyCell.addGestureRecognizer(tap)
tap.cancelsTouchesInView = false
Run Code Online (Sandbox Code Playgroud)
isUserInteractionEnabledMyView本身或其任何父母可能是错误的。
小智 -1
尝试在里面设置委托
func awakeFromNib(){
super.awakeFromNib()
self.collectionView.dataSource = self
self.collectionView.delegate = self }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7835 次 |
| 最近记录: |