Zap Dojin
Zap Dojin

Reputation: 75

Can't send image from array to collectionViewCell

I've collection view and cell configured from array, I want to set the image of cell's UIImageView from the array, but xCode returns an error saying: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value, I don't know if it's helpful, but I can't even open the tab where collectionView and its cell is located, the app crashes immediately

//collectionViewCell
    @IBOutlet weak var headerTitle: UILabel!
    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var countryImage: UIImageView!
    func config(data:SearchCollectionViewData){
        self.headerTitle.text = data.header
        self.infoLabel.text = data.information
        self.countryImage.image = data.image // error goes here
    }
//collectionView and its configuration
    var array2:[SearchCollectionViewData] = [
        SearchCollectionViewData(header: "Georgia,Tbilisi Airport", information: "Georgia is located between Asia and Europe and occupies a land area of 69,700 square kilometres, bordered by the Black Sea to the west, Turkey to the southwest, Azerbaijan to the east, Russia to the north, and Armenia to the south. ... Georgian is the official language of Georgia, and it is spoken by 71% of the population.", image: UIImage(named: "tbtbtb")!)
]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
}
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "searchCell", for: indexPath) as! searchCollectionViewCell
            cell.config(data: array2[indexPath.row])
}

Upvotes: 0

Views: 37

Answers (1)

Zap Dojin
Zap Dojin

Reputation: 75

The main problem was @IBOutlet weak var countryImage: UIImageView!, where the error was, it wasn't connected to storyboard

Upvotes: 1

Related Questions