首页 > 解决方案 > 在同一 ViewController 的第二个 CollectionView 中选择单元格时更新一个 UICollectionView

问题描述

我在 Swift 中工作,并且有一个 ViewController 作为数据源和 2 个 UICollectionView 实例的委托。我想根据选择了collectionView1中的哪个项目来更改collectionView2使用的数据源,并在collectionView1中进行新选择时刷新collectionView2中显示的项目。

我假设用户的选择或多或少会像这样传播:

CollectionView1Cell -> ViewController -> CollectionView2

如何提醒 ViewController 在第一个 CollectionView 中进行了新选择,以便它可以更新第二个 ViewController 的单元格?

标签: iosswiftuicollectionview

解决方案


你说你有你的 UICollectionViews 与视图控制器作为委托和数据源,有这个集合视图的委托方法:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

每次选择集合视图项时,它都会为您提供一个 collectionView 和一个 IndexPath,在其他其他情况下,您可以这样做:

假设您的第一个集合视图称为firstCollectionView您可以执行以下操作

if collectionView == self.firstCollectionView{ let selectedData = firstCollectionData[indexPath.row] //do something with the selected data to affect the secondCollectionView datasource self.modifySecondCollectionView(with: selectedData) //call the reloadData method for the second collection view in order to display the changes self.secondCollectionView.reloadData() }

希望对你有帮助,祝你好运!


推荐阅读