我在Swift中工作,有一个ViewController用作数据源和2个UICollectionView实例的委托。我想根据在collectionView1中选择了哪个项目来更改用于collectionView2的数据源,并在collectionView1中进行新选择时刷新在collectionView2中显示的项目。
我认为用户的选择或多或少会像这样传播:
CollectionView1Cell-> ViewController-> CollectionView2
如何通知ViewController在第一个CollectionView中进行了新选择,以便可以更新第二个ViewController的单元格?
答案 0 :(得分:1)
您说过,您有两个UICollectionViews都以视图控制器作为委托和数据源,对于集合视图有以下委托方法:
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath)
,每次选择一个集合视图项时,它都会为您提供一个collectionView和一个IndexPath,在其他情况下,您可以这样做:
假设您的第一个收藏夹视图称为 firstCollectionView ,您可以按照以下步骤操作
if collectionView == self.firstCollectionView {
让selectedData = firstCollectionData [indexPath.row]
//对所选数据执行某些操作以影响secondCollectionView数据源
self.modifySecondCollectionView(with:selectedData)
//调用第二个集合视图的reloadData方法以显示更改
self.secondCollectionView.reloadData()
}
希望有帮助,祝您好运!