【问题标题】:IB Outlet for NativeAd is nil in a tableview cellNativeAd 的 IB Outlet 在 tableview 单元格中为零
【发布时间】:2021-02-22 02:37:57
【问题描述】:
  let nativeAdCell = tableView.dequeueReusableCell(
      withIdentifier: "Ads", for: indexPath) as! NativeAds

然后

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

运行时,出现错误,说明线程 1:致命错误:在展开 Optional 值时意外发现 nil。这意味着视图为零

编辑 1

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


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

我是怎么注册的,我用的是 Xib 文件

【问题讨论】:

  • 你是如何注册你的单元类的?你在用故事板吗?你设置好插座了吗?表格视图是否来自那个故事板?为什么要强制将属性向下转换为UILabel?该属性应该已经具有正确的类型。如果它有不同的类型,那么强制向下转换也可能导致崩溃。
  • 对其进行了编辑并感谢信息
  • 你必须register the NIB 而不是班级。如果您从类中实例化,则未设置出口。

标签: ios swift xcode uitableview xib


【解决方案1】:

按照下面的代码在表格视图中注册XIB文件:

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

【讨论】:

  • 它说“[ setValue:forUndefinedKey:]: 这个类不符合键视图的键值编码。”
  • 可能是插座坏了。检查您班级中的插座连接以及 XIB 文件连接检查器。
  • 修复了问题,请大家帮忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 2014-01-11
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多