7

I have a table view with custom cell that was created as .xib . I didnt use storyboard. I have a problem that I couldnt fill my table with the data which came from webservice result. Also, I have 4 labels in the custom cell. In my custom cell class, when I try to set labels for each items, It gives me fatal error like above.

Here is my code:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
...

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

    if let ip = indexPath
    {
        let item: Item = self.itemList[indexPath.row] as Item
        cell.setCell(item.itemName, status: item.itemStatus, duration: item.itemDuration, price: item.itemPrice)
    }
    return cell
}
}

And my custom cell class is here :

import UIKit

class ItemManagementTVCell: UITableViewCell {

    @IBOutlet var lblItemName: UILabel!
    @IBOutlet var lblItemPrice: UILabel!
    @IBOutlet var lblItemDuration: UILabel!
    @IBOutlet var lblItemStatus: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }

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

    func setCell(name: String, status: Int, duration: Int, price: Int)
    {
        self.lblItemName.text     = name
        self.lblItemStatus.text   = String(status)
        self.lblItemDuration.text = "Duration: \(String(duration)) months"
        self.lblItemPrice.text    = String(price) + " $"
    }
}

I am getting the error inside of "setCell" method block.

I have read a lot of questions and solutions and I tried all of them it doesnt work for me.

Thank you for your answers,

Best regards.

SOLUTION: I've solved this problem by linking the cell items to cell's own instead of linking to File's Owner. My problem has gone by doing this.

serhanaksut
  • 622
  • 3
  • 7
  • 23
  • Can you add the solution here? – neosergio Nov 26 '14 at 20:31
  • This is very simple, I'll explain it step by step 1-) Click your cell 2-) click the connections inspector which is located in "Hides or show Utilities" menu on the right side. 3-) Click the File's Owner, if there is any connected items, delete all of them. Then click cell view and connect your cell items there. Note: I did not use storyboard. This solution for xibs. @neosergio – serhanaksut Nov 27 '14 at 13:17
  • thank you for the clarification – neosergio Nov 27 '14 at 19:51
  • I had a similar problem with this code getting "fatal error, unexpectedly found nil". But it was crazy because if I inserted... print(indexPath) print(tableView) I could clearly see that neither was nil. Even worse, it was an intermittent problem. Sometimes it worked and other times it didn't. – Dave G Feb 24 '16 at 15:02

2 Answers2

9

Another solution to the problem without having to link cell items to the cell owner:

let nib = UINib(nibName: "YOUR_CUSTOM_CELL_NIB_NAME", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "YOUR_CUSTOM_CELL_ID")
Lawliet
  • 3,212
  • 1
  • 14
  • 25
7

Your "cell" must be nil.

Using

tableView.dequeueReusableCellWithIdentifier("cell") as ItemManagementTVCell

Can return nil. You should use:

tableView.dequeueReusableCellWithIdentifier("cell" forIndexPath:indexPath) as ItemManagementTVCell

This way it guarantees cells is not nil.

EDIT: Maybe you can prevent the crash by putting if statements inside "setCell"

if var itemName = self.lblItemName {
    itemName.text = name
}

Do that for every label you set inside it and check if the crash still happens. If it don't you must check why those labels are nil.

Thiago Magalhaes
  • 859
  • 9
  • 11
  • Can you put a breakpoint inside setCell block and check if something inside is "nil"? – Thiago Magalhaes Aug 29 '14 at 13:58
  • I think the problem is related to labels becasue when I initialize the labels, it doesnt throw any exception, but there is not any item in tableview. It is empty. Are there any error related to labels ? – serhanaksut Aug 29 '14 at 14:05
  • I edited the answer with a possible solution. Check that please. – Thiago Magalhaes Aug 29 '14 at 14:14
  • I have tried, but it doesnt work still :( Thank you for your answers. – serhanaksut Aug 29 '14 at 14:24
  • 1
    Check those links and see if it helps: http://stackoverflow.com/questions/24643522/swift-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value http://stackoverflow.com/questions/24715343/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value-in-swift – Thiago Magalhaes Aug 29 '14 at 14:25