The output of both print statements is nil.
Here is the part in the ViewController responsible for the UICollectionView:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allCars.count + 1 // +1 due to addCar cell, otherwise first car is not shown
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 { // makes sure addCar cell is shown
let addCell = collectionView.dequeueReusableCell(withReuseIdentifier: "addCar", for: indexPath)
return addCell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Car", for: indexPath) as! CarSelectionCollectionViewCell
cell.nameLabel.text = allCars[indexPath.row-1].name // -1 in orter to display all cars. Otherwise first car would not be shown and index out of range
if let imgData = allCars[indexPath.row-1].photo as Data?
{
cell.picImageView.image = UIImage(data: imgData)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! CarSelectionCollectionViewCell
impactGenerator.impactOccurred()
UIView.animate(withDuration: 0.1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: {
cell.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
},
completion: { finished in
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
cell.transform = CGAffineTransform(scaleX: 1, y: 1)
}, completion: nil)
})
// EVERYTHING IS UPDATED ACCORDING TO SELECTED CAR
selectedCar = allCars[indexPath.row-1] // selectedCar is set to newly selected car
appDelegate.updateSelectedCarRefuelEntries()
//cell.picImageView.layer.add(animation, forKey: "pulsing")
tableView.reloadSections(NSIndexSet(index: 0) as IndexSet, with: .fade) // reloads tableView with animation
indexOfSelectedCar = indexPath.row-1
defaults.set(indexOfSelectedCar, forKey: "indexOfSelectedCar")