-1

I ask for help with determining the right approach

when I click on the picture I need to pass text and her to new VC, but the problem is that the project has a great depth struct

the cell lies in the collectionView, which is in the tableViewCell

How do I properly implement the transfer? (the number of cells is generated automatically)

Another complication is that the code of the cell with which I work lies in another cell.

those. the transfer of information must also be written in it.

import UIKit
import moa

class RealPhotoInfoTableViewCell: UITableViewCell {

    @IBOutlet private weak var MyCollection: UICollectionView!

    var model: RealPhoto!

    func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
        MyCollection.delegate = self
        MyCollection.dataSource = self
        MyCollection.reloadData()
    }
}

extension RealPhotoInfoTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(model?.data.count as Any)
        return model?.data.count ?? 0
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RPhotoCell", for: indexPath) as! RealPhotoCollectionViewCell

        cell.realPhotoImage.moa.url = model?.data[indexPath.row].photo

        return cell
    } 
}
Сева
  • 510
  • 1
  • 4
  • 15
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – arvidurs May 29 '19 at 17:46

3 Answers3

1

Add a var

class RealPhotoInfoTableViewCell: UITableViewCell {
   weak var delegate:VCName?

Inside cellForRowAt of the table do

cell.delegate = self

Now you can do this inside didSelectItemAt of the collection

delegate?.sendData(image)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • thanks, I tried to do as you describe But it does not come out. I will update the question, please take a look. – Сева May 30 '19 at 08:02
  • @Сева `cell.delegate = self` should be inside the tableView `cellForRowAt` not the collection – Shehata Gamal May 30 '19 at 10:11
  • it still doesn't work. in the first VC where I have a tableview, I have prescribed `realPfotoCell.delegate = self` and have an error `Cannot assign value of type 'DetailViewController' to type 'ImagesViewController?'` – Сева May 30 '19 at 13:03
0

You have two options: One is Sh_Khan answer using delegation.

Or you can use Notification observers.

This question has been asked before...

arvidurs
  • 2,853
  • 4
  • 25
  • 36
0

You can use delegate protocol in that case. You can implement that protocol. Please find following sample code for that.

protocol ImageSelectionDelegate {
    func imageClicked(imageURL: String)
}

//Class A
class A : ImageSelectionDelegate{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //your existing code
        cell.delegate = self
    }
    //Implement that protocol
    func imageClicked(imageURL: String){
        //You will get url for tapped image
    }
}

class RealPhotoInfoTableViewCell{
    weak var delegate : ImageSelectionDelegate? = nil
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate.imageClicked(model?.data[indexPath.item].photo)
    }
}