从单元格 collectionView 中的点击中放松

unwind from tap in cell collectionView

我已经尝试了几个小时,但我不明白我希望有人能帮助我!

实际上我有一个初始 VC 我点击一张图片,通过关注将我带到一个 collectionView,这两个元素也由一个 NC 加入。

在 CollectionView 中插入了图像,这些图像包含在一个数组中,我想将图像触摸到 return 到初始 VC 并且显示的图像是选定的图像在 CollectionView 中。

我试过使用UnWind,但是我无法在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
    }

    ...
}

即使设置索引 = indexPath.row 它也永远不会在展开时恢复

您已将 unwind segue 连接到您的集合视图单元格。展开发生在 didSelectItemAt 运行之前。

您可以通过以下两种方式之一解决此问题:

  1. 从单元格中删除 segue,并将其从 viewController 图标(左上角图标)连接到退出图标(右上角图标)。在 Document Outline 中找到这个 exit segue 并给它一个标识符,例如 "returnToMain"。在didSelectItemAt中,设置indice后调用self.performSegue(withIdentifier: "returnToMain", sender: self)

  2. 根本不要使用 didSelectItemAt。相反,实施 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
            }
        }
    }
    

您应该为此目的实施一些处理程序或委托方法。例如:

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