didSelectItemAt()未加载我的viewController

时间:2018-07-18 10:52:17

标签: ios swift4

第一个viewController

   import UIKit

  class photosCollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var photoCollectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    photoCollectionView.delegate = self
    photoCollectionView.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

var photosArray = PhotoClass(Name:["Dog","Cat","Lion","Tiger"])
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photosArray.Name.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let photocell = photoCollectionView.dequeueReusableCell(withReuseIdentifier: "photosCell", for: indexPath) as! photoscollectionCell
    //photocell.backgroundColor = UIColor.blue
    photocell.cellImage.image = UIImage(named: photosArray.Name[indexPath.row])
    photocell.cellLabel.text = photosArray.Name[indexPath.row]

    return photocell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("Something")
    let  vc = self.storyboard?.instantiateViewController(withIdentifier: "photosDetails") as! detailsCollectionViewController
   // vc.descriptionLabel.text = "Animals"

}
}

didSelectItemAt()不会加载我的detailsCollectionViewController,但是当我选择单元格时,它正在调用.so,我在哪里做错了?

2 个答案:

答案 0 :(得分:1)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("Something")
    let  vc = self.storyboard?.instantiateViewController(withIdentifier: "photosDetails") as! detailsCollectionViewController
   // vc.descriptionLabel.text = "Animals"
    present(vc, animated: true)
}

答案 1 :(得分:1)

如果要推送,可以编写以下代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("Something")
    let  vc = self.storyboard?.instantiateViewController(withIdentifier: "photosDetails") as! detailsCollectionViewController
   // vc.descriptionLabel.text = "Animals"
  self.navigationController?.pushViewController(vc, animated: true)
}

这可能对您有所帮助。谢谢