我正在尝试在我的 UICollectionView 中选择一个单元格,我已经完成了所有工作,包括设置委托(delegate)和数据源,我的 ScrollView 上没有点击手势识别器。
我在 ScrollView 中有一个 View ,我将其子类化为 UIControl 以在用户点击屏幕退出键盘时处理,但我也删除了它并且 didSelectItemAt 仍然没有触发。 这是我的代码:
@IBOutlet weak var templatesCollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
templatesCollection.delegate = self
templatesCollection.dataSource = self
}
extension AddCardViewController : UICollectionViewDelegate,UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return templates.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = templatesCollection.dequeueReusableCell(withReuseIdentifier: "template", for: indexPath) as! TemplateCollectionViewCell
cell.templateView.text = templates[indexPath.row]
cell.alignTextVertically()
cell.isUserInteractionEnabled = true
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)
cell.layer.shadowRadius = 2.0
cell.layer.shadowOpacity = 0.5
cell.layer.masksToBounds = false
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("shfgdsfgidks")
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("Deselected")
}
}
此外,值得一提的是, Collection View 正在显示数据,因此我知道 cellForItemAt 函数至少正在触发,只有 didSelectItemAt 没有被调用。
为 collectionView 及其单元启用了用户交互。 另外,这是我的层次结构的照片
最佳答案
所以 UICollectionView
和 UITableView
都继承自 UIScrollView
并且不应该放在另一个 scrollView 中。 Apple 表示这是一个坏主意,会导致问题。所以最好的答案是不要那样做。
通常,如果您需要这样做,通常最好在 UITableViewCell
中嵌入一个 UICollectionView
来创建您的布局。不知道你到底想做什么,很难提出最好的方法
如果您必须这样做,那么您需要查看UIGestureRecogniserDelegate并配置他们可以同时识别的特定手势。
关于ios - UICollectionView didSelectRowAt 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477272/