Search code examples
iosswiftxcodeuitableviewxib

IB Outlet for NativeAd is nil in a tableview cell


  let nativeAdCell = tableView.dequeueReusableCell(
      withIdentifier: "Ads", for: indexPath) as! NativeAds

then

 (nativeAdCell.HeadlineView).text = nativeAd.headline
        (nativeAdCell.PriceView).text = nativeAd.price
        

When running, an error occurs stating Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. which means the view is nil

edit 1

   tableview.register(NativeAds.self, forCellReuseIdentifier: "Ads")


   let nativeAdCell = tableView.dequeueReusableCell(
       withIdentifier: "Ads", for: indexPath) as! NativeAds

Is how I registered it, I'm using an Xib file for this


Solution

  • Follow the code below to register XIB file in table view:

    tableView.register(UINib(nibName: "yourNibFileName", bundle: nil), forCellReuseIdentifier: "yourIdentier")