CollectionView didSelectItem 没有响应。为什么?
CollectionView didSelectItem does not respond. Why?
首先,我确实设置了代表。其他所有协议都在使用集合视图。
我用 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。编辑器不识别"extension ViewController"部分是代码正常吗?
只需删除 CollectionViewCell 并创建一个新的即可解决。
我仍然不知道我之前的 collectionViewCell 有什么问题,因为除了 didSelectItemAt..
如果有人遇到熟悉的问题并且知道发生了什么,我仍然很高兴听到发生了什么;)
首先,我确实设置了代表。其他所有协议都在使用集合视图。 我用 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。编辑器不识别"extension ViewController"部分是代码正常吗?
只需删除 CollectionViewCell 并创建一个新的即可解决。
我仍然不知道我之前的 collectionViewCell 有什么问题,因为除了 didSelectItemAt..
如果有人遇到熟悉的问题并且知道发生了什么,我仍然很高兴听到发生了什么;)