如何从 tableView 单元格内的 collectionView 的 didSelectItemAt 导航到 viewController?

How can I Navigate to a viewController from a collectionView's didSelectItemAt which is inside a tableView cell?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        //let cell: HomeCollectionViewCell = homeCollectionView.dequeueReusableCell(withReuseIdentifier: "homeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "ReadOrDownloadViewController") as! ReadOrDownloadViewController

        navigationController.pushViewController(vc, animated: true)

    }

这里Xcode说“使用未解析的标识符'navigationController'”

HomeCollectionViewCell 在 tableViewCell 下。

您必须为单元格 class 设置委托或闭包才能将操作传递给 viewcontroller

关闭:

class myCell: UITableViewCell{
   var closure: (() -> Void)?

由于您将此 class 设置为数据源和 collectionView 的委托,因此将此函数重写为

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    closure?()
}

然后在 viewController 中设置 tableview 数据源,

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.deque ....
cell.closure = {_ in
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "ReadOrDownloadViewController") as! ReadOrDownloadViewController
        navigationController.pushViewController(vc, animated: true)

因为协议是相同的,只是在单元格中设置委托并在 collectionView didTap 项目函数中调用委托函数