繁体   English   中英

将数据从一个 collectionView 传递到另一个 collectionView

[英]Passing data from one collectionView to other collectionView

UICollectionView ,我正在显示图像并单击它在 ViewController 中打开的图像。 工作正常。

但是,当图像在新的 viewController 中打开时,我想向右/向左滑动图像。

为了滑动图像,我尝试在DetailViewController添加一个CollectionView并在单元格内添加UIImageView

//on clicking the image from main view controller

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController
        vc?.name = imgArr[indexPath.row]
        self.navigationController?.pushViewController(vc!, animated: true)
    }

错误是:

“非法配置:从 DetailViewController 到 UIImageView 的 img 插座无效。插座不能连接到重复的内容。”

最简单的方法是将整个imgArr作为DetailViewController collectionView 的数据源传递。 然后在DetailViewController定义selectedName属性并在DetailViewController出现之前滚动到所选名称。

编辑:

//on clicking the image from main view controller

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController {
        vc.imgArr = imgArr
        vc.selectedNameIndex = imgArr[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM