Swift 如何通过点击TableViewCell中的CollectionViewCell来使用NavigationController

Swift 如何通过点击TableViewCell中的CollectionViewCell来使用NavigationController,swift,Swift,我想按下ViewController并显示有关该单元格的详细信息 迅捷5 import UIKit class ViewController: UIViewController { @IBOutlet weak var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() myTableView.delegate = self m

我想按下ViewController并显示有关该单元格的详细信息

迅捷5

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myTableView: UITableView!

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

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return ViewController.typeOfDishes.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return ViewController.typeOfDishes[section]
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.myTableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
        return cell
    }
}





import UIKit

class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var myCollectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        myCollectionView.delegate = self
        myCollectionView.dataSource = self
    }
}

extension MyTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return ViewController.dishesCountByArray[ViewController.indexOfTableView!]
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
////////////There I want to do PushViewController Action        
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.myCollectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
        return cell
    }
}

我想去​sh当用户点击集合视图单元格时,在此添加一个委托

class MyTableViewCell: UITableViewCell {
   weak var delegate:ViewController?
   .....
}
然后把它放在这里

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = self.myTableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
   cell.delegate = self
   return cell
}
现在你可以推这里了

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
   let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondID") as! SecondViewController  
   self.delegate?.navigationController?.push////
}

在这里添加一个委托

class MyTableViewCell: UITableViewCell {
   weak var delegate:ViewController?
   .....
}
然后把它放在这里

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = self.myTableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
   cell.delegate = self
   return cell
}
现在你可以推这里了

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
   let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondID") as! SecondViewController  
   self.delegate?.navigationController?.push////
}

当我选择一个单元格时,我会看到黑屏:让vc=SecondViewController()self.delegate?.navigationController?.pushViewController(vc,动画:true)它显示secondVc在情节提要中,所以在您将情节提要标识符
SecondID
设置为itBy情节提要后,将其作为编辑加载。InstanceViewController(带标识符:)顺便问一下?如果是,没有故事板如何创建secondVC?如果以编程方式在ViewDidLoad中更改视图的背景,当我选择一个单元格时,我会看到黑屏:让vc=SecondViewController()self.delegate?.navigationController?.pushViewController(vc,动画:true)看起来secondVc在情节提要中,因此,在您将序列图像板标识符
SecondID
设置为itBy storyboard之后,将其作为编辑加载。通过这种方式实例化EviewController(带标识符:)?如果是,没有故事板如何创建secondVC?如果是以编程方式,则在viewDidLoad中更改其视图的背景