如何使用UIColletcionView将数据从TableViewCell内部的UICollectionViewCell传递到另一个ViewController

时间:2018-08-02 10:15:00

标签: ios swift uitableview uicollectionview uicollectionviewcell

我有3个班级:MainViewControllerMagazineTableViewCellMagazineCollectionViewCell。请检查故事板的屏幕截图-

看看我的MainViewController

class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "magazineCell", for: indexPath) as! MagazineTableViewCell

        self.magazine = cell.magazine

        return cell
    }
     else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "newInWomenCell", for: indexPath) as! NewInWomenTableViewCell

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewInMenCell", for: indexPath) as! NewInMenTableViewCell

        return cell
    }

我的TableViewCell班级:

class MagazineTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    var magazine = [Magazine]()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

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

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

        cell.set(magazine: magazine[indexPath.row])

        return cell
    }

还有我上一堂课UICollectionViewCell

class AllMagazineCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var image: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        image.clipsToBounds = true
        image.layer.cornerRadius = 30
        backgroundColor = UIColor.clear

    }

    func set(magazine: Magazine) {
        ImageService.getImage(withURL: magazine.imageOfMagazine) { image in
            self.image.image = image
        }
    }

}

我如何将数据从UICollectionViewCell传递到另一个视图控制器?

Func为segue做准备现在在MainViewController中起作用,在MagazineTableViewCell中,该函数未被调用。

对不起,我的工程师。

2 个答案:

答案 0 :(得分:0)

1-

class MagazineTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

   @IBOutlet weak var collectionView: UICollectionView!

   weak var main: MainViewController!

2-

class AllMagazineCollectionViewCell: UICollectionViewCell {

  @IBOutlet weak var image: UIImageView!

  weak var main: MainViewController!

3-

let cell = tableView.dequeueReusableCell(withIdentifier: "magazineCell", for: indexPath) as! MagazineTableViewCell
cell.main = self

4-

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

5-现在,单击单元格时,您可以将其与任何数据一起进行主初始化,并可以使用它来执行segue

main.performSegue////

在collectionCell内

答案 1 :(得分:-1)

您不应将数据从UICollectionViewCell直接传递到另一个视图控制器,这不是一个好的设计。您必须从MagazineTableViewCellMainViewController进行委派或完成处理程序回调,然后才能从MainViewController传递数据到另一个控制器。

在下面这样使用:

class MagazineTableViewCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {
      var cellSelectionHandler: ((yourData: type) -> Void)? // Callback handler
}

然后在MagazineTableViewCell类中实现集合视图didSelectItemAtIndexPath,然后从那里调用cellSelectionHandler并将所需的数据传递给处理程序。

并按如下所示在cellSelectionHandler中设置MainViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "magazineCell", for: indexPath) as! MagazineTableViewCell

        self.magazine = cell.magazine

        // Handle your callback here
        cell.cellSelectionHandler = { (yourData) in
           // Handle your data here and pass to another ViewController.
        }

        return cell
    }
     else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "newInWomenCell", for: indexPath) as! NewInWomenTableViewCell

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewInMenCell", for: indexPath) as! NewInMenTableViewCell

        return cell
    }

完成处理程序示例: 例如,您需要将Int值传递给完成处理程序:

//处理程序声明

var cellSelectionHandler: ((index: Int) -> Void)?

//处理程序定义

cell.cellSelectionHandler = { (index) in
// use the index here
}

//从didSelectItemAtIndexPath的处理程序调用

cellSelectionHandler?(index) // pass your index of type Int

任何疑问,请发表评论。

相关问题