Как открыть новый контроллер для UICollectionViewCell - PullRequest
0 голосов
/ 28 мая 2020

Я следую https://www.freecodecamp.org/news/autolayout-programmatically-spotify-clone-in-swift/, чтобы создать 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 изображения и заголовка Мне нужно щелкнуть каждую ячейку, чтобы открыть страницу контроллера, чтобы показать каждую деталь меню.

1 Ответ

1 голос
/ 28 мая 2020

Передайте ссылку weak var на ваш контроллер в HomeView. Затем используйте didSelectItemAt метод UICollectionView внутри HomeView, вот пример:

weak var homeViewController: HomeViewController?

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let newViewController = UIViewController()
    newViewController.view.backgroundColor = .purple
    homeViewController?.navigationController?.pushViewController(newViewController, animated: true)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...