如何解决Swift UICollectionView 不调用方法 didSelectItemAt
我一直在尝试创建自定义 collectionView,但由于某种原因,方法 [MY-LINK](../../SOME-OTHER-README.MD)
不起作用。
我将 func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath)
添加到控制器中,但不起作用。
这里有一些代码:
UICollectionViewDelegate
我对 Swift 开发还很陌生,我不知道我在这里缺少什么。
提前致谢
编辑 1:
添加所有控制器代码
解决方法
我检查了你的代码。委托模式是正确的。 我认为,您必须实现数据源功能,以便集合视图可以包含一些项目。 根据当前代码,集合视图中没有项目。 所以 didSelectItemAt 事件现在不能发生。 请参考以下代码。
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell",for: indexPath) as! MyCell
cell.textLabel.text = String(indexPath.row + 1)
return cell
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
print(indexPath.item + 1)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。