gpt4 book ai didi

ios - didSelectItemAtIndexPath 未在同一屏幕上使用多个 Collection View 调用

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:45 24 4
gpt4 key购买 nike

我在同一个屏幕上有 2 个 Collection View ,并且我在同一个 View Controller 中为它们实现了数据源和委托(delegate)。但是,didSelectItemAtIndexPath 等委托(delegate)方法只被调用一次。

其他信息:

  • 两个 Collection View 都有一个带有图像的单元格。
  • AllowSelection 和 UserInteractionEnabled 在两个 Collection View 中都设置为 true
  • 在图像上启用用户交互
  • 委托(delegate)和数据源在 Storyboard上从两个 Collection View 到 View Controller 设置
  • Collection View 正确显示

两个 Collection View 具有相同的设置,但只有一个委托(delegate)工作。您有什么线索可以设置它吗?

Collection View 位于具有 ScrollView 的 View 内。这会以某种方式相关吗?

编辑 2:

有同样问题的项目:https://github.com/iagomr/ProblemWithAutoLayout

编辑 1:

这与自动布局约束有关,因为如果我将底部 Collection View 固定到屏幕底部而不是其他 Collection View 的底部,它就会开始工作。

这都是因为我需要构建一个高屏幕,并将所有内容添加到 1000 点高的 ScrollView 中的 View 中。

代码:

//MARK: - CollectionView Delegate
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

println("Called")
}

//MARK: - CollectionView DataSource
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == thisCV {
return 1

} else if collectionView == thisOtherCV{
return 1
}
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

if collectionView == "thisCV" {
if let thisCell = collectionView.dequeueReusableCellWithReuseIdentifier("thisCell", forIndexPath: indexPath) as? thisCollectionViewCell {

thisCell.image = image

return thisCell
}

} else if collectionView == "thisOtherCV"{
if let thisOtherCell = collectionView.dequeueReusableCellWithReuseIdentifier("thisOtherCell", forIndexPath: indexPath) as? OtherCollectionViewCell {

thisOtherCell.image = image

return thisOtherCell
}
}

return UICollectionViewCell()
}

最佳答案

我可以确认 didSelectItem 没有被调用。如果将两个 Collection View 之间的上下约束常量从 501 更改为 0,则它可以正常工作。

这个问题很可能与您在另一个 ScrollView 中有两个 ScrollView ( Collection View )有关。总的来说,我会说,你应该修改你的用户界面。我会建议两种修复方法

使用单一 Collection View

只使用一个 Collection View ,其中包含针对不同内容的不同部分。另外,不要将它嵌入到 ScrollView 中—— Collection View 已经有一个 ScrollView ,所以你应该能够轻松滚动。您还可以为不同的部分出列不同类别的单元格,以便您现在应该能够做任何您想做的事情。

如果你想要一个起点,here是一个很好的教程,应该可以帮助您。

使用 ScrollView

如果您想在 Interface Builder 中设置您的 UI,请删除两个 Collection View 并简单地将所有 UI 添加到 ScrollView 中。将 UIButton 放置在您希望通过点击产生操作的位置。

您甚至可以为每个按钮分配相同的操作,然后通过为每个按钮分配自定义标签来区分触发的是哪个按钮。

关于ios - didSelectItemAtIndexPath 未在同一屏幕上使用多个 Collection View 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com