【发布时间】:2019-06-25 21:34:55
【问题描述】:
我试图在我的 UICollectionView 中选择一个单元格,我已经完成了所有工作,包括设置委托和数据源,我的滚动视图上没有点击手势识别器。
我在滚动视图中有一个视图,当用户点击屏幕退出键盘时,我将其子类化为 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")
}
}
另外值得一提的是,集合视图正在显示数据,所以我知道至少 cellForItemAt 函数正在触发,它只有 didSelectItemAt 未被调用。
collectionView 及其单元格都启用了用户交互。 另外,这是我的层次结构的图片
【问题讨论】:
-
所以你有一个包含全屏集合视图的视图,没有其他滚动视图,任何地方都没有点击手势识别器?没有子视图添加到集合视图?
-
我自己写的。
-
@AkshayRaina 在情节提要中,请检查
User Interaction Enabled是否为UICollectionView和UICollectionViewCell打勾。 -
不,实际上我有一个带有注册表单的滚动视图,最后我有一个可水平滚动的集合视图,任何地方都没有点击手势识别器,并且集合视图单元格内部有一个 textView
-
@AkshayRaina 您没有获得点击,因为点击是针对 TextView。如果您需要对 didSelect 进行操作,则禁用单元格内的 TextView 的用户交互
标签: ios swift xcode uicollectionview