TableView Cell Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

238 views Asked by At

I have been using the same protocols and functions throughout all projects. But this one is giving me hard times with fatal error.

This is my Nibloadable

 protocol NibLoadable: class {
    static var nib: UINib { get }
}

extension NibLoadable {
    static var nib: UINib {
        return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
    }
}

This is my Reusable

    protocol Reusable: class {
    static var reuseIdentifier: String { get }
}

extension Reusable {
    static var reuseIdentifier: String {
        return String(describing: Self.self)
    }
}

in VideoListTableViewCell

static let identifier = String(describing: VideoListTableViewCell.self)
extension VideoListTableViewCell: Reusable, NibLoadable { }

inside my VideoViewController

class VideoViewController: UIViewController, VideoModule.View  {
@IBOutlet weak var tableView: UITableView!
var presenter: VideoModule.Presenter!
var videos: [VideoModel]?

override func viewDidLoad() {
        super.viewDidLoad()
        
        presenter.fetchVideos()
        configureTableView()
    }
func configureTableView() {
        tableView.register(VideoListTableViewCell.self)
        tableView.delegate = self
        tableView.dataSource = self

extension VideoViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return configureTableViewCell(indexPath: indexPath)
    }
    
    func configureTableViewCell(indexPath: IndexPath) -> VideoListTableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: VideoListTableViewCell.identifier, for: indexPath) as! VideoListTableViewCell
        let videoData = (videos?[indexPath.row])!
        cell.configure(model: videoData)
        return cell
    }

So inside configureTableView function at .register it throws fatal error. I have tried adding an identifier to create my cell and register tableView but it also did not solve this problem.

at the .register line my tableView is tableView UITableView? nil none with the error explanation.

And finally my UITableView Extension

extension UITableView {
  
    func register<T: UITableViewCell>(_: T.Type) where T: Reusable, T: NibLoadable {
        register(T.nib, forCellReuseIdentifier: T.reuseIdentifier)
    }
    
    func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: Reusable {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            fatalError("Could not dequeue cell with identifier \(T.reuseIdentifier)")
        }
        return cell
    }

I have tried using VIPER so this is my VideoContract

 protocol VideoModuleViewProtocol: AnyObject {
  var presenter: VideoModule.Presenter! { get set }
  
}

protocol VideoModuleInteractorProtocol: AnyObject {
  var presenter: VideoModule.Presenter? { get set }
  
  func fetchVideos()
}

protocol VideoModulePresenterProtocol: AnyObject {
  var view: VideoModule.View? { get set }
  var interactor: VideoModule.Interactor! { get set }
  var router: VideoModule.Router! { get set }

    func fetchVideos()
    func didFetch(videos: [VideoModel])
}

protocol VideoModuleRouterProtocol: AnyObject {
  
}

struct VideoModule {
  typealias View = VideoModuleViewProtocol
  typealias Interactor = VideoModuleInteractorProtocol
  typealias Presenter = VideoModulePresenterProtocol
  typealias Router = VideoModuleRouterProtocol
}

I have created storyboard file as VideoListTableViewCell.xib and created both tableviewcell files at the same time. Everything is connected to the correct places too.

How can I overcome this meaningless problem?

0

There are 0 answers