Simple UITableView in Swift - unexpectedly found nil
Solution 1
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
Solution 2
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.
Solution 3
I was getting this error because I didn't have the identifier written in the Storyboard of the Custom Cell.
Also make sure it matches you code in:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
...
}
Solution 4
Possibly that your view in Main.Storyboard lost its IBOutlet reference in ViewController file, just link it again.
Solution 5
Do not forget to register nib (tested with Swift3), e. g. inside override func viewDidLoad():
self.tableView.register(UINib(nibName: "BookTableViewCell", bundle: nil), forCellReuseIdentifier: "BookCell")
Related videos on Youtube
soleil
Updated on February 27, 2020Comments
-
soleil almost 3 years
Pretty simple code:
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 }
On the cell.bookLabel.text line I get this:
fatal error: unexpectedly found nil while unwrapping an Optional value
The BookTableViewCell is defined like this:
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 } }
And bookLabel is correctly hooked up in a Prototype cell in the Storyboard. Why am I getting this error?
-
Vincil Bishop about 8 yearsThis was the right answer for me...was pulling my hair out.
-
Cesare over 5 yearsThanks so much. I was so used to write this when I was doing everything programmatically.
-
Adam Fendley almost 5 yearsIn my case I had a stale IBOutlet reference that was causing this error. Thanks for the suggestion.
-
Giovanni Palusa over 4 yearsFinally a good answer on this question - I spent the last hour of trying to figure this one out...
-
Lax about 4 yearsInstead, to register the cell, use
self.tableView.register(UINib(nibName: LXIssueConstants.issueCell, bundle: nil), forCellReuseIdentifier: LXIssueConstants.issueCell)
-
stepheaw over 3 yearsThis was the case using Xamarin iOS storyboards. I just needed to clear the TableView name property, save the storyboard. Re-enter the TableView name, and then delete bin and obj folders