Simple UITableView in Swift - unexpectedly found nil

后端 未结 13 1507
旧巷少年郎
旧巷少年郎 2020-12-06 00:38

Pretty simple code:

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

func tableView(tableView:UITableView!, numberOfRows         


        
相关标签:
13条回答
  • 2020-12-06 01:27

    In my case it was the way the Optional is unwrapped:

    let cellId:String = "ConverterTableCell"
    let cell: ConverterTableViewCell = (tableView.dequeueReusableCellWithIdentifier(cellId)! as? ConverterTableViewCell)!
    
    0 讨论(0)
  • 2020-12-06 01:28

    I am getting this error whenever I use reuse Identifer name different than the custom class name unifid those names solve it for me

    0 讨论(0)
  • 2020-12-06 01:29

    If you're using storyboard, make sure you don't have this line at the start of your file:

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

    It will overwrite the storyboard and as a result, the outlet links in the storyboard are ignored.

    0 讨论(0)
  • 2020-12-06 01:30

    When you create a view in code, its IBOutlet properties don't get hooked up properly. You want the version that you get back from dequeueReusableCellWithIdentifier:

    let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell
    
    0 讨论(0)
  • 2020-12-06 01:32

    I encounter this error, if I put UITapGestureRecognizer in a custom UITableViewCell on the storyboard.(Xcode version is 8.3).

    0 讨论(0)
  • The reason why this question gets asked a lot is because it depends on how you setup your tableview and custom CellClass. Do you create your tableview in storyboard or programmatically? Do you create custom .xib Cells and custom Cell classes?

    If you created your tableview programmatically and created custom .xib and Cell class here is the answer for Swift 4:

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

    Note: In your cell .xib file make sure you set your identifier in the Attributes inspector ("NibNameIdentifier").

    0 讨论(0)
提交回复
热议问题