【问题标题】:passing image from collectionViewCell which is located tableviewCell从位于 uitableviewCell 的集合 View Cell 中传递图像
【发布时间】:2019-05-29 17:32:34
【问题描述】:

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

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

单元格位于collectionView,即tableViewCell

如何正确实施转移? (单元格数自动生成)

另一个复杂因素是我工作的单元格的代码位于另一个单元格中。

那些。信息的传递也必须写在里面。

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

【问题讨论】:

标签: swift uitableview uicollectionview


【解决方案1】:

添加变量

class RealPhotoInfoTableViewCell: UITableViewCell {
   weak var delegate:VCName?

在表的cellForRowAt里面做

cell.delegate = self

现在您可以在集合的didSelectItemAt 内执行此操作

delegate?.sendData(image)

【讨论】:

  • 谢谢,我试着按照你描述的做,但它没有出来。我会更新问题,请看一下。
  • @Сева cell.delegate = self 应该在 tableView cellForRowAt 而不是集合中
  • 还是不行。在我有一个 tableview 的第一个 VC 中,我规定了 realPfotoCell.delegate = self 并且有一个错误 Cannot assign value of type 'DetailViewController' to type 'ImagesViewController?'
【解决方案2】:

您有两个选择: 一种是使用委托的 Sh_Khan 回答。

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

这个问题之前有人问过...

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多