ios - 在同一 ViewController 的第二个 CollectionView 中选择单元格时更新一个 UICollectionView
问题描述
我在 Swift 中工作,并且有一个 ViewController 作为数据源和 2 个 UICollectionView 实例的委托。我想根据选择了collectionView1中的哪个项目来更改collectionView2使用的数据源,并在collectionView1中进行新选择时刷新collectionView2中显示的项目。
我假设用户的选择或多或少会像这样传播:
CollectionView1Cell -> ViewController -> CollectionView2
如何提醒 ViewController 在第一个 CollectionView 中进行了新选择,以便它可以更新第二个 ViewController 的单元格?
解决方案
你说你有你的 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()
}
希望对你有帮助,祝你好运!
推荐阅读
- python - 将一个文件 Tkinter 条目分配给另一个文件 tkinter 条目
- react-native - 如何制作圆形倒计时/仪表以显示反应原生的剩余天数
- python - 使用 AutoSys 运行 python 脚本时出错
- terraform - 重用在terraform的父模块中声明和定义的变量
- python - 过滤python请求结果
- database - MongoDB/Mongoose:对同一集合中不同类型的文档使用不同的方案
- android - 如何在 Android 上的 Xamarin.forms 应用程序中呈现动态生成的地图图钉?
- c# - 自动更新 WinForms 应用程序(异步)
- c# - 如果 readalltext 找不到文件,请创建文件
- android - android 10 aosp 中哪里有 3 个点?