Swift 从位于tableviewCell的collectionViewCell传递图像

Swift 从位于tableviewCell的collectionViewCell传递图像,swift,uitableview,uicollectionview,Swift,Uitableview,Uicollectionview,我请求帮助确定正确的方法 当我点击图片时,我需要将文本和她传递给新的VC,但问题是这个项目有一个很好的深度结构 该单元格位于tableViewCell中的collectionView中 我如何正确实施转移? 单元格的数量是自动生成的 另一个复杂的情况是,我工作的细胞的代码位于另一个细胞中 那些。信息的传递也必须写入其中 import UIKit import moa class RealPhotoInfoTableViewCell: UITableViewCell { @IBOutl

我请求帮助确定正确的方法

当我点击图片时,我需要将文本和她传递给新的VC,但问题是这个项目有一个很好的深度结构

该单元格位于tableViewCell中的collectionView中

我如何正确实施转移? 单元格的数量是自动生成的

另一个复杂的情况是,我工作的细胞的代码位于另一个细胞中

那些。信息的传递也必须写入其中

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
    } 
}
添加一个变量

class RealPhotoInfoTableViewCell: UITableViewCell {
   weak var delegate:VCName?
在桌边的牢房里

cell.delegate = self
现在,您可以在集合的didSelectItemAt中执行此操作

delegate?.sendData(image)
您有两个选择: 一个是Shu Khan使用委托回答

或者您可以使用通知观察员


这个问题以前被问过…

在这种情况下,您可以使用委托协议。您可以实现该协议。请找到下面的示例代码

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

可能是重复的谢谢,我试着按照你描述的做,但没有出来。我将更新问题,请看一看。@СССаcell.delegate=self应该在tableView单元格中,而不是集合中。它仍然不起作用。在我有tableview的第一个VC中,我指定了realPfotoCell.delegate=self,并且出现了一个错误,无法将“DetailViewController”类型的值分配给“ImagesViewController”类型