从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现一个 ViewController [英] Presenting a ViewController from within a CollectionViewCell that is nested in a TableViewCell

查看:12
本文介绍了从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现一个 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从嵌套在 TableViewCell 中且 TableViewCell 位于 ViewController 中的 CollectionViewCell 中呈现 ViewController?

How can I present a ViewController from within a CollectionViewCell that is nested in a TableViewCell and that TableViewCell is inside a ViewController?

我尝试了 presentViewControllerperformSegue,但它们无法从单元格内调用.

I have tried presentViewController and performSegue but they can't be called from within a cell.

详细说明:

我有三个文件.

  1. ProfileViewController
  2. PeopleCategoryTableViewCell
  3. PeopleCategoryCollectionViewCell

当有人点击 CollectionViewCell 中的故事时,我希望他们重定向到另一个 ViewController,即 SinglePostVC()

When someone taps on a story in CollectionViewCell then I want them to redirect to another ViewController i.e. SinglePostVC()

我尝试过的:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("inside this")
    self.window?.rootViewController?.present(SinglePostVC(), animated: true, completion: nil)
    print("outside this")
}

推荐答案

像这样使用协议和委托

tableViewCell

protocol CellDelegate {
    func colCategorySelected(_ indexPath : IndexPath)
}

var 委托:CellDelegate?

var delegate : CellDelegate?

在 didSelect 中

In didSelect

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

    delegate?.colCategorySelected(indexPath)
}

在你的 ProfileViewController

In your ProfileViewController

class HomeVC: UIViewController , UITableViewDelegate, UITableViewDataSource, CellDelegate{
:
:

func colCategorySelected(_ indexPath : IndexPath){
    // Push here
  }
:
:
}

不要忘记

cellForRow

   let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! tableCell

     Cell.delegate = self // dont forget this line.
     return Cell

这篇关于从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现一个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆