【问题标题】:How to push a view from inside a UITableViewCell that is in a UICollectionViewCell?如何从 UICollectionViewCell 中的 UITableViewCell 内部推送视图?
【发布时间】:2018-12-30 11:31:26
【问题描述】:

我在UICollectionViewCell 内部有一个UITableView,我正在尝试从tableView(_:didSelectRowAt:) 方法内部的UITableViewCell 推送一个新视图。但是,我无法访问navigationController。我将如何导航到新视图?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var item = Item(name: "")

    switch indexPath.section {
    case 0:
        item = array1![indexPath.item]
    case 1:
        item = array2![indexPath.item]
    default:
        break
    }

    let layout = UICollectionViewFlowLayout()
    let newView = NewCollectionView(collectionViewLayout: layout)
    newView.itemOfInterest = item

    // Can't reference navigationController
}

【问题讨论】:

  • 您可以创建将在 didSelect 中触发的委托方法,并且它应该在您可以推送的视图控制器中实现。
  • 你也可以在你的ViewController中实现tableview委托的delete方法。而且您还可以选择在您的 ViewController 上调用自定义委托。

标签: ios swift uitableview uinavigationcontroller


【解决方案1】:

获取当前View的父控制器-

//MARK: get parent controller...
extension UIView {
    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}

用法:- 获取 navigationController 引用为parentViewController?.navigationController

let viewController = GymLearnMoreViewController(nibName: "GymLearnMoreViewController", bundle: nil) //Your View controller instance
parentViewController?.navigationController?.pushViewController(viewController, animated: true)

来源苹果:-

https://developer.apple.com/documentation/uikit/uiresponder/1621099-next https://developer.apple.com/documentation/uikit/uiresponder?changes=_9

【讨论】:

    【解决方案2】:

    为此

    1 .你需要在添加tableview的collectionviewcell中声明一个协议

    2 。在添加collectionview的类上实现协议方法。并从这里处理点击导航

    3 .调用 indexpath 处的 collectionviewCellForrow 方法时设置 collectionview 单元格委托,并在 collectionview 单元格内单击 tableview 项时调用委托方法

    【讨论】:

      【解决方案3】:

      一种方法是使用NotificationCenter,你可以写成ParentViewController

      override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(methodNavigate(notification:)), name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
      }
      
      @objc func methodNavigate(notification: NSNotification) {
          Write your push segue code here.
      }
      

      别忘了删除viewDidDisappear中的观察者:

      override func viewDidDisappear(_ animated: Bool) {
          super.viewDidDisappear(animated)
      
          NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
      }
      

      您也可以通过它传递数据。

      然后在didSelectRowAt

      NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: "Send Data Here")
      

      但我会建议使用协议是更好和优雅的方式。 您可以在互联网上获得大量教程和示例:

      希望对您有所帮助。编码愉快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-31
        • 2011-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 2018-02-04
        • 2020-11-24
        相关资源
        最近更新 更多