17

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?

soleil
  • 12,133
  • 33
  • 112
  • 183

13 Answers13

85

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.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
mliu
  • 1,708
  • 15
  • 26
20

I was getting this error because I didn't have the identifier written in the Storyboard of the Custom Cell.

StoryBoard

Also make sure it matches you code in:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell

        ...
    }
uplearned.com
  • 3,393
  • 5
  • 44
  • 59
  • Sometimes Xcode behave weird, even after adding identifier if issue persists, you can build project and issue is fixed – Mrinmoy Apr 21 '18 at 06:47
9

Possibly that your view in Main.Storyboard lost its IBOutlet reference in ViewController file, just link it again.

Hieu Vo
  • 3,105
  • 30
  • 31
  • In my case I had a stale IBOutlet reference that was causing this error. Thanks for the suggestion. – Adam Fendley Feb 16 '18 at 05:34
  • This 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 – stepheaw Oct 05 '19 at 03:54
7

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
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
3

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")
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
3

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").

Alex Bailey
  • 793
  • 9
  • 20
3

You need to check two things

1. Register cell with nib name in viewDidLoad


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

2. Create custom cell this way.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ListProductCell") as! ListProductCell
        return cell
    }
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
2

Swift 5 In same storyboard two class are there Class A and Class B, Class B contains tableview outlet, when i tried to push Class A to Class B it's crashed and show tableView outlet nil.

In class A i did navigation like below code.

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

Then i realised my mistake and used below code and it's work perfectly.

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

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

let cellId:String = "ConverterTableCell"
let cell: ConverterTableViewCell = (tableView.dequeueReusableCellWithIdentifier(cellId)! as? ConverterTableViewCell)!
Dani.Rangelov
  • 366
  • 2
  • 5
1

Try doing this:

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

and don't forget to set the reuse identifier in your storyboard

Brigadier
  • 886
  • 7
  • 17
0

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

Dania Delbani
  • 816
  • 1
  • 11
  • 27
0

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

Photon Point
  • 798
  • 2
  • 13
  • 23
0

In Swift 5

First I tried registering my UITableViewCell in viewdidload() using class and identifier which I have mentioned below but it did not work for me.

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

Solution

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

Register your cell in viewdidload() using Nib name

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")
}
ABS
  • 7,634
  • 1
  • 21
  • 20