Simple UITableView in Swift - unexpectedly found nil

ios, swift, uitableview

Solution

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

Problem

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?

Original source