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,我在哪里做错了?
答案 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)
}
这可能对您有所帮助。谢谢