I am trying to make a app that when you click the button on collectionview cell, you can show the detail viewcontroller and gives more information about your choice.But all attributes of my struct shown as nil on other viewController.
Here is my ViewController including CollectionView
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var ilceDizisi = [Ilceler]()
var secim : Ilceler?
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font:UIFont(name: "Papyrus", size: 25.0)]
navigationController?.navigationBar.titleTextAttributes = textAttributes as [NSAttributedString.Key : Any]
//Hucre Boyurlandirmasi
let tasarim = UICollectionViewFlowLayout()
let genislik = self.collectionView.frame.size.width
tasarim.minimumInteritemSpacing = 5
tasarim.minimumLineSpacing = 5
tasarim.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
tasarim.itemSize = CGSize(width: (genislik - 30) / 2, height: 322.0)
collectionView.collectionViewLayout = tasarim
ilceDizisi = [Ilceler.adalar, Ilceler.arnavutkoy, Ilceler.atasehir, Ilceler.avcilar, Ilceler.bagciler, Ilceler.bahcelievler, Ilceler.bakirkoy, Ilceler.basaksehir, Ilceler.bayrampasa, Ilceler.besiktas, Ilceler.beykoz, Ilceler.beylikduzu , Ilceler.beyoglu, Ilceler.buyukcekmece, Ilceler.catalca , Ilceler.cekmekoy , Ilceler.esenler , Ilceler.esenyurt , Ilceler.eyupsultan , Ilceler.fatih , Ilceler.gaziosmanpasa , Ilceler.gungoren , Ilceler.kadikoy , Ilceler.kagithane , Ilceler.kartal, Ilceler.kucukcekmece , Ilceler.maltepe , Ilceler.pendik , Ilceler.sancaktepe , Ilceler.sariyer , Ilceler.sile , Ilceler.sultanbeyli , Ilceler.sultangazi , Ilceler.sile, Ilceler.sisli , Ilceler.tuzla , Ilceler.umraniye , Ilceler.uskudar , Ilceler.zeytinburnu]
}
}
extension ViewController : UICollectionViewDelegate , UICollectionViewDataSource, CellButton {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ilceDizisi.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hucre", for: indexPath) as! Cell
cell.isimLabel.text = ilceDizisi[indexPath.row].isim!
cell.yakaLabel.text = ilceDizisi[indexPath.row].yaka!
cell.ilceGorsel.image = UIImage(named: ilceDizisi[indexPath.row].gorsel!)
cell.prot = self
cell.indexPath = indexPath
return cell
}
func detail(indexPath: IndexPath) {
performSegue(withIdentifier: "toDetailVC", sender: nil)
secim = ilceDizisi[indexPath.row]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailVC" {
let destVC = segue.destination as! DetailViewController
destVC.secilenIlce = secim
}
}
}
Here is my detail viewController
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var baskan: UILabel!
@IBOutlet weak var yuz: UILabel!
@IBOutlet weak var nufus: UILabel!
@IBOutlet weak var yaka: UILabel!
@IBOutlet weak var gorselView: UIImageView!
@IBOutlet weak var ilceLabel: UILabel!
var secilenIlce : Ilceler?
override func viewDidLoad() {
super.viewDidLoad()
baskan.text = "Belediye Başkanı : \(secilenIlce?.belediyeBaskani)"
yuz.text = "Yüzölçümü(km²) : \(secilenIlce?.yuzolcumu)"
nufus.text = "Nüfus : \(secilenIlce?.nufus)"
yaka.text = "Bulunduğu Yaka : \(secilenIlce?.yaka)"
gorselView.image = UIImage(named: (secilenIlce?.gorsel)!)
ilceLabel.text = secilenIlce?.isim
}
}
I want to see informations like this
but when i runned the project
I tried to get all informations with prepareforsegue func.Actually i needed to delete imageview.image code due to the fact that it returns nil and my project crashs if i unwrapped it.What is my fault and how can i fix it
CodePudding user response:
You need to set secim
before performing the segue. Swap the two lines in your detail
function.
func detail(indexPath: IndexPath) {
secim = ilceDizisi[indexPath.row]
performSegue(withIdentifier: "toDetailVC", sender: nil)
}
With your original code you were calling performSeque
which results in the call to prepare
. But at that point you haven't set secim
. So you were setting secim
after prepare
was being called. Most likely, if you tapped on a cell again, it would then show the data of the previously tapped cell.