【问题标题】:Presenting a ViewController from within a CollectionViewCell that is nested in a TableViewCell从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController
【发布时间】:2018-03-06 09:43:03
【问题描述】:

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

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

详细解释:

我有三个文件。

  1. ProfileViewController
  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")
}

【问题讨论】:

    标签: ios swift uitableview uicollectionview presentviewcontroller


    【解决方案1】:

    像这样使用协议和委托

    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
    

    【讨论】:

    • 在步骤 2 中出现错误。使用未解析的标识符 'delegate'
    • 无法声明 cell.delegate=self 即最后一步错误 - 无法将 ProfileViewController 类型的值分配给 CellDelegate 类型
    • 您需要创建协议,然后在您的ProfileViewController 中创建一个名为“delegate”var delegate : CellDelegate? 的变量来保存引用。然后您可以将您的单元格的委托分配为该变量。如果您不熟悉协议-委托模式,我真的建议您研究并完全学习它。它在 Cocoa/iOS 编程中随处可见。
    • 还要确保您的 ProfileViewController 订阅了 CellDelegate : Class ProfileViewController : UIViewController, CellDelegate
    • 谢谢。它现在正在运行,但我也正在重定向它的 vc 在这一行显示错误 - singlePostTable.dataSource = self singlePostTable.delegate = self ...“在展开可选时意外发现 nil”
    【解决方案2】:

    您可以从 PeopleCategoryCollectionViewCell 发出通知并在 ProfileViewController 中监听它,然后您可以正常呈现视图控制器。

    本地通知教程:https://useyourloaf.com/blog/local-notifications-with-ios-10/

    例子:

    NSNotificationCenter
            .defaultCenter()
            .postNotificationName(kCellTappedKey, object: nil, userInfo: nil)
    

    然后听:

    notificationCenter.addObserver(self,
                                       selector: #selector(self.onCellTapped(_:)),
                                       name: kDocumentCellTappedKey,
                                       object: nil)
    

    并添加此功能:

    func onCellTapped(notification: NSNotification) { //call perform segue }
    

    【讨论】:

    • 谢谢你的回答..你能告诉我代码吗?我是一个新手,我被困了几个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多