Swift 如何打开UICollectionViewCell的新控制器

Swift 如何打开UICollectionViewCell的新控制器,swift,uicollectionview,Swift,Uicollectionview,我必须创建uicollectionview,但不同的是我使用UIView来实现。但是,我不知道如何单击Sub-UICollectionCell的每个单元格并打开一个新的UIController。下面是我的代码 Class HomeView : UIView,UICollectionViewDataSource,UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout{ lazy var collectionView :

我必须创建uicollectionview,但不同的是我使用UIView来实现。但是,我不知道如何单击Sub-UICollectionCell的每个单元格并打开一个新的UIController。下面是我的代码

Class HomeView : UIView,UICollectionViewDataSource,UICollectionViewDelegate   ,UICollectionViewDelegateFlowLayout{
    lazy var collectionView : UICollectionView = {
      let layout = UICollectionViewFlowLayout()
      let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
      cv.translatesAutoresizingMaskIntoConstraints = false
      cv.delegate = self
      cv.dataSource = self
      cv.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
      cv.backgroundColor = UIColor(hexString: "#F7F7F7")
      return cv
    }()
    override init(frame: CGRect) {
      super.init(frame: frame)
      addSubview(collectionView)

    }

   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return sections.count
   }


   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)  -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeCollectionViewCell
      cell.section = sections[indexPath.item]

      return cell
    }
}

我有HomeCollectionCell作为

class HomeCollectionViewCell : UICollectionViewCell , UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
   override init(frame: CGRect) {
      super.init(frame: frame)
      addSubview(titleLabel)
      collectionView.dataSource = self
      collectionView.delegate = self
      collectionView.register(SubHomeViewCell.self, forCellWithReuseIdentifier: cellId)
      setupSubCells()
    }

  fileprivate  func setupSubCells(){
       // add collectionView to the view
       addSubview(collectionView)

       collectionView.dataSource = self
       collectionView.delegate = self

   }

   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return  self.subCategorys.count
   }
   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SubHomeViewCell
       cell.subCategory = subCategorys[indexPath.item]
       return cell
   }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

       let width = frame.height
       let height = frame.height

       return CGSize(width: width, height: height)

  }
}

对于我的SubHomeView,我只添加了图像和标题的addSubview
我需要单击每个单元格以打开控制器页面以显示每个菜单详细信息。

HomeView
中传入对控制器的
弱var
引用。然后,在
HomeView
中使用
UICollectionView
方法,下面是一个示例:

弱变量homeViewController:homeViewController?
重写func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
让newViewController=UIViewController()
newViewController.view.backgroundColor=.purple
homeViewController?.navigationController?.pushViewController(newViewController,动画:true)
}

这适用于HomeView中的UICollectionView,但我想在单击子UICollectionView(HomeCollectionViewCell中的UICollectionView)时打开控制器。您有什么想法来实现它吗?将homeVC作为弱变量传递给您的
子UICollectionView