简体   繁体   中英

unwind from tap in cell collectionView

I've been trying for hours but I do not understand how I hope someone can help me!

In practice I have an initial VC I click on an image that takes me to a collectionView through a follow and the 2 elements are also joined by a NC.

In the CollectionView are inserted images, which are contained in an array, I would like to touch on an image to return to the initial VC and that the image displayed is the one selected in the CollectionView.

I tried with UnWind but I can not carry the information of the image index that I try to recover in the didselct.

Viewcontroller

class ViewController: UIViewController {

    @IBOutlet weak var immagine: UIImageView!

    @IBAction func actionTap(_ sender: UITapGestureRecognizer) {
        print("tap")
        performSegue(withIdentifier: "selezione", sender: nil)
    }

    @IBAction func indietro(segue: UIStoryboardSegue){

        let recupero = segue.source as! CollectionViewController
        print(recupero.indice)

        immagine.image = UIImage(named: recupero.arrayImmagini[recupero.indice])
    }

    private let reuseIdentifier = "Cell"

    ...
}

CollectionViewController

class CollectionViewController: UICollectionViewController {

    var arrayImmagini = ["globe","bed","home","toolbox"]
    var indice = 0

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayImmagini.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
        cell.imgCell.image = UIImage(named: arrayImmagini[indexPath.row])
        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //   print(arrayImmagini[indexPath.row])
        let indice = indexPath.row
        //   idImg = indexPath.row
    }

    ...
}

even if set index = indexPath.row it is never recovered in the unwind

You have wired the unwind segue to your collection view cell. The unwind happens before didSelectItemAt runs.

You can fix this in one of two ways:

  1. Remove the segue from the cell, and wire it from the viewController icon (top left icon) to the exit icon (top right icon). Find this exit segue in the Document Outline and give it an identifier such as "returnToMain" . In didSelectItemAt , call self.performSegue(withIdentifier: "returnToMain", sender: self) after setting indice .

    OR

  2. Don't use didSelectItemAt at all. Instead, implement prepare(for:sender:) :

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "returnToMain" { if let indexPath = self.collectionView.indexPathsForSelectedItems?.first { indice = indexPath.row } } } 

You should implement some handler for this purpose or delegate method. For example:

class ViewController: UIViewController {
    ...
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "yourCollectionViewController" {
            if let vc = segue.destination as? CollectionViewController {
                vc.imageHandler = { imageIndex in
                    ...
                }
            }
        }
    }
}

class CollectionViewController: UICollectionViewController {
    var imageHandler : ((_ imageId: Int) -> Void)?
    ...

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       //   print(arrayImmagini[indexPath.row])
        let indice = indexPath.row
        //  idImg = indexPath.row
        imageHandler?(indice)
        dismiss(animated: true, completion: nil)
    }
}

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