简体   繁体   中英

Passing data from one collectionView to other collectionView

In a UICollectionView , I am displaying images and on clicking the image it opens up in a ViewController. Works fine.

However, I want to swipe the images to right/left when the image opens in the new viewController.

To swipe the images, I tried adding a CollectionView in the DetailViewController and added UIImageView inside the cell.

//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)
    }

The error is:

"Illegal Configuration: The img outlet from the DetailViewController to the UIImageView is invalid. Outlets cannot be connected to repeating content."

the easiest way for you will be to pass the whole imgArr as a data source for the collectionView in the DetailViewController . Then define a selectedName property in the DetailViewController and scroll to the selected name before DetailViewController has appeared.

EDIT:

//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)

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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