【问题标题】:Swift UIcollectionView change content of second UICollectionView on same ViewController?Swift UIcollectionView 在同一个 ViewController 上更改第二个 UICollectionView 的内容?
【发布时间】:2019-01-15 01:03:01
【问题描述】:

我是集合视图的新手,我正在努力让一个集合视图选择单元格来更改另一个集合视图的内容显示的内容,这两个集合视图都在同一个 ViewController 上。

我在一个视图控制器上有两个工作集合视图,并希望我在第一个集合视图上的选择来更改第二个集合视图的内容。

按照 Apple 文档,当我像往常一样设置 collectionViews 时,觉得我不需要发布我的 collectionViews 代码。

所以两个集合视图都可以工作,只是这个请求的功能还没有。

数据示例如下:

收藏查看一

Let arrayDrinks = [“beer”, “spirit”, “wine”]

集合视图二(根据集合视图一中选择的内容而变化)

Let arrayBeer = [“Carling”, “fosters”, “Carlsberg”]
Let arraySpirit = [“vodka”, “whiskey”]
Let arrayWine = [“red wine”, “white wine”, rose]

任何帮助都会很棒。谢谢

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

    实现集合视图的委托方法。

    extension YourViewController: UICollectionViewDelegate {
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
            if collectionView == firstCollectionView {
               // Identify the selected cell here and change in your second collection view accordingly. 
            }
        }
    }
    

    别忘了在你的视图控制器中设置委托:

    firstCollectionView.delegate =  self 
    secondCollectionView.delegate =  self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多