ios - 从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController

标签 ios swift uitableview uicollectionview presentviewcontroller

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

我已经尝试过 presentViewControllerperformSegue 但它们无法从单元格中调用。

详细解释:

我有三个文件。

  1. 配置文件 View Controller
  2. PeopleCategoryTableViewCell
  3. PeopleCategoryCollectionViewCell

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

我尝试过的:

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

最佳答案

像这样使用协议(protocol)和委托(delegate)

tableViewCell

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

var delegate : CellDelegate?

在didSelect中

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

    delegate?.colCategorySelected(indexPath)
}

在你的 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

关于ios - 从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404897/

相关文章:

ios - 如何在不添加 subview 的情况下实现UITableview seperator 'above first row'

iphone - 沿曲线滚动

ios - Facebook SDK v4和Parse SDK

arrays - 如何快速访问字典中包含的数组元素

ios - 如何将两个 UIImage 添加到 UITableView 背景

iphone - 将导航 Controller 添加到现有的 UITableView

android - Win 7、安卓、iOS SDK

ios - UIViewController 的子类所需的初始值设定项

ios - 无法 swift 推断通用参数

Swift 类型 'any View' 无法符合 'View' 协议(protocol)