【发布时间】:2020-03-16 04:46:15
【问题描述】:
首先,我确实设置了代表。所有其他协议都在使用集合视图。 我用 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。
希望你们中的一个人找到一个明显的原因,为什么这不起作用。我很高兴知道为什么:)
谢谢。
附言。编辑器不将“扩展ViewController”部分识别为代码是否正常?
【问题讨论】:
-
我忘了说,所有用户交互都已启用。如果我让 collectionView 可滚动,它工作得很好。
-
didSelectItem没有回应(比你的问题标题说的)或cellForItemAt(那么你的问题里面是什么)? -
我不确定你的意思。如果我触摸collectionViewItem,我只想得到回应。在这种情况下,如果我触摸一个项目。他需要通过打印“做某事”来回应
-
你在你的问题中问过 但是为什么 cellForItem 函数根本没有响应? 你的意思是
didSelectItemAt? -
您的扩展定义似乎有点奇怪,因为您将扩展添加到 ViewController 类而不是特定的类。我建议您将扩展添加到作为您的 Collection View 代表的 viewController
标签: ios swift delegation