【问题标题】:Simple UITableView in Swift - unexpectedly found nilSwift 中的简单 UITableView - 意外发现 nil
【发布时间】:2014-09-10 02:05:02
【问题描述】:

很简单的代码:

func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    return 1
}

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
    return 5
}


func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
    let cell: BookTableViewCell = BookTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BookCell")
    println("ip: \(indexPath.row)")
    cell.bookLabel.text = "test"

    return cell
}

在 cell.bookLabel.text 行我得到这个:

fatal error: unexpectedly found nil while unwrapping an Optional value

BookTableViewCell 是这样定义的:

class BookTableViewCell: UITableViewCell {

    @IBOutlet var bookLabel: UILabel

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

并且 bookLabel 正确连接到情节提要的原型单元格中。为什么我会收到此错误?

【问题讨论】:

标签: ios uitableview swift


【解决方案1】:

斯威夫特 5 在同一个情节提要中,有两个类是 A 类和 B 类,B 类包含 tableview 出口,当我试图将 A 类推到 B 类时,它崩溃并显示 tableView 出口为零。

在 A 类中,我做了如下代码的导航。

let classBObj = ClassB()
self.navigationController?.pushViewController(classBObj, animated: true)

然后我意识到我的错误并使用下面的代码,它工作得很好。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let classBObj = storyboard.instantiateViewController(withIdentifier: "ClassB") as! ClassB
self.navigationController?.pushViewController(classBObj, animated: true)

【讨论】:

    【解决方案2】:

    你需要检查两件事

    1。在 viewDidLoad 中使用 nib 名称注册单元


    func viewDidLoad() 
    {
        super.viewDidLoad()
        listTableView.register(UINib.init(nibName: "ListProductCell", bundle: nil), forCellReuseIdentifier: "ListProductCell")
     }
    

    2。以这种方式创建自定义单元格。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ListProductCell") as! ListProductCell
            return cell
        }
    

    【讨论】:

      【解决方案3】:

      在 Swift 5 中

      首先,我尝试使用下面提到的类和标识符在 viewdidload() 中注册我的 UITableViewCell,但它对我不起作用。

      self.tableView.registerClass(MyCustomTableViewCell.self, forCellReuseIdentifier: "customCell")
      

      解决方案

      Then I registered my UITableViewCell using Nib name and it worked for me
      

      使用 Nib 名称在 viewdidload() 中注册您的单元格

      override func viewDidLoad() 
      {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
      
          //register your table view cell in Viewdidload() using Nib Name       
          tableView.register(UINib.init(nibName: "MyCustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
      }
      

      【讨论】:

        【解决方案4】:

        这个问题被问到很多的原因是因为它取决于你如何设置你的 tableview 和自定义 CellClass。您是在情节提要中还是以编程方式创建表格视图?您是否创建自定义 .xib Cell 和自定义 Cell 类?

        如果您以编程方式创建了 tableview 并创建了自定义 .xib 和 Cell 类,那么这就是 Swift 4 的答案:

        in viewDidLoad:
        
                customTable.register(UINib(nibName: "NibName", bundle: nil), forCellReuseIdentifier: "NibNameIdentifier")
        
        in cellforRowat:
                let cell = tableView.dequeueReusableCell(withIdentifier: "NibName") as! ClassName
        

        注意:在您的单元格 .xib 文件中,确保您在属性检查器(“NibNameIdentifier”)中设置了您的标识符。

        【讨论】:

          【解决方案5】:

          如果我将 UITapGestureRecognizer 放在故事板上的自定义 UITableViewCell 中,我会遇到此错误。(Xcode 版本为 8.3)。

          【讨论】:

            【解决方案6】:

            不要忘记注册 nib(使用 Swift3 测试),例如。 G。 覆盖函数 viewDidLoad()

            self.tableView.register(UINib(nibName: "BookTableViewCell", bundle: nil), forCellReuseIdentifier: "BookCell")
            

            【讨论】:

              【解决方案7】:

              每当我使用 reuse Identifer 名称而不是 自定义类名称 时,我都会收到此错误 统一这些名字为我解决了它

              【讨论】:

                【解决方案8】:

                尝试这样做:

                let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookTableViewCell
                

                别忘了在故事板中设置重用标识符

                【讨论】:

                  【解决方案9】:

                  在我的例子中,这是 Optional 的展开方式:

                  let cellId:String = "ConverterTableCell"
                  let cell: ConverterTableViewCell = (tableView.dequeueReusableCellWithIdentifier(cellId)! as? ConverterTableViewCell)!
                  

                  【讨论】:

                    【解决方案10】:

                    可能您在 Main.Storyboard 中的视图在 ViewController 文件中丢失了其 IBOutlet 引用,只需重新链接即可。

                    【讨论】:

                    • 在我的情况下,我有一个过时的 IBOutlet 引用导致此错误。感谢您的建议。
                    • 这是使用 Xamarin iOS 故事板的情况。我只需要清除 TableView 名称属性,保存情节提要。重新输入TableView名称,然后删除bin和obj文件夹
                    【解决方案11】:

                    我收到此错误是因为我没有在自定义单元的情节提要中写入标识符。

                    还要确保它与您的代码匹配:

                    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
                        {
                    
                            let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
                    
                            ...
                        }
                    

                    【讨论】:

                    • 有时 Xcode 的行为很奇怪,即使在添加标识符后,如果问题仍然存在,您可以构建项目并修复问题
                    【解决方案12】:

                    如果您使用情节提要,请确保文件开头没有此行:

                    self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
                    

                    它将覆盖情节提要,因此情节提要中的出口链接被忽略。

                    【讨论】:

                    • 这对我来说是正确的答案......正在拔掉我的头发。
                    • 非常感谢。当我以编程方式做所有事情时,我已经习惯了写这个。
                    • 这个问题终于有了一个很好的答案——我花了最后一个小时试图弄清楚这个问题......
                    • 相反,要注册单元格,请使用self.tableView.register(UINib(nibName: LXIssueConstants.issueCell, bundle: nil), forCellReuseIdentifier: LXIssueConstants.issueCell)
                    【解决方案13】:

                    当您在代码中创建视图时,它的IBOutlet 属性没有正确连接。您想要从dequeueReusableCellWithIdentifier 获得的版本:

                    let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2020-11-27
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2021-09-09
                      • 2020-03-28
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多