IVAN KHRAMOV
IVAN KHRAMOV

Reputation: 13

Unexpectedly found nil while implicitly unwrapping an Optional value (UICollectionView)

I want to make collection view that reloads its data after getting new items. But when im trying to reload collection view im getting this error

Here is a code of View controller

import UIKit

class GalleryViewController: UIViewController {
var presenter : ViewToPresenterPhotoProtocol?

@IBOutlet var collectionView: UICollectionView!

let reuseIdentifier = "cell"

override func viewDidLoad() {
    super.viewDidLoad()
    presenter?.viewDidLoad()
    // Do any additional setup after loading the view.
}
}

extension GalleryViewController: UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.presenter?.photos?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
    
    cell.label.text = self.presenter?.photos?[indexPath.item].name
    cell.backgroundColor = UIColor.yellow
    
    return cell
}

// MARK: - UICollectionViewDelegate protocol

private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}
}

extension GalleryViewController: PresenterToViewPhotoProtocol{
func onFetchPhotoSuccess() {
    self.collectionView.reloadData()
}

func onFetchPhotoFailure(error: String) {
    print("View receives the response from Presenter with error: \(error)")
}


}

I don't know how to properly reload data in storyboard's collection view so I'll be pretty thankful if you'll help me solving this issue

Upvotes: 1

Views: 805

Answers (3)

stoikokolev
stoikokolev

Reputation: 517

  1. Set identifier in Attributes Inspector

  2. Register your custom cell.

     @IBOutlet private var collectionView: UICollectionView! {
         didSet {
             collectionView.register(PhotoCollectionViewCell.self,   
                                     forCellWithReuseIdentifier: reuseIdentifier)
         }
     }
    

Upvotes: 1

sonle
sonle

Reputation: 9020

Maybe your GalleryViewController was initialized without xib/storyboard.

If you're using storyboard try this:

let storyboard = UIStoryboard(name: 'yourStoryboardName', bundle: .main)
let controller = storyboard.instantiateViewController(identifier: "yourControllerIdentifier") as! GalleryViewController

If you're using xib try this:

let controller = UIViewController(nibName: "yourControllerNibName", bundle: .main) as! GalleryViewController

Upvotes: 1

West1
West1

Reputation: 1918

You've force-converted a value that is nil. You'll need to use either as or as?, I believe.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? PhotoCollectionViewCell
    
    ...
}

Upvotes: 0

Related Questions