CollectionView didSelectItem不响应。为什么?

时间:2019-11-20 12:52:39

标签: ios swift delegation

首先,我确实设置了代表。其他所有协议都在使用集合视图。 我用sizeFotItem函数进行了测试。而且效果很好。但是,为什么cellForItem函数根本不响应?

有人知道为什么吗?

这是我的代码;

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layoutSettings(flow)
    playButtonPressed(self)
}

这是在我的viewController扩展中;

    extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 9
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCell
            print(menu.menuArray[indexPath.row])
            cell.cellText.text = menu.menuArray[indexPath.row]
            return cell
        }


        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("Do something")
        }
    }

有趣的部分是..我在另一个项目中具有几乎相同的代码,并且每个工作都很好。 唯一的区别是项目在TableView内有一个collectionView。

希望你们中的一个发现这样做不明显的明显原因。我很高兴知道为什么:)

谢谢。

PS。编辑器无法将“扩展ViewController”部分识别为代码是否正常?

1 个答案:

答案 0 :(得分:0)

仅删除CollectionViewCell并制作一个新的即可解决。

我仍然不知道以前的collectionViewCell出了什么问题,因为除didSelectItemAt之外,每种协议都工作正常。

如果有人遇到一个熟悉的问题并且知道发生了什么,我仍然很高兴听到发生了什么;)