0

This is the custom class for my cell:

class showSugTableViewCell: UITableViewCell {


override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
    super.init(style: .Default, reuseIdentifier: "showSug")
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func awakeFromNib() {
    super.awakeFromNib()


    let categoryLbl = UILabel()
    categoryLbl.frame = CGRectMake(20, 10, 100, 20)
    categoryLbl.text = "Text"
    contentView.addSubview(categoryLbl)



}

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


}

}

And here's the code from cellForRowAtIndexPath:

   let cell = tableView.dequeueReusableCellWithIdentifier("showSug") as! showSugTableViewCell


        return cell

The problem is that my app crashes on the cell initialisation, and the log says:

fatal error: unexpectedly found nil while unwrapping an Optional value

How can I solve this?

EDIT:

I changed my code to this:

   let cellTemp = self.sugTableView.dequeueReusableCellWithIdentifier("showSug")
        var cell = cellTemp as? showSugTableViewCell!

        if cell == nil {
            self.sugTableView.registerNib(UINib(nibName: "showSugTableViewCell", bundle: nil), forCellReuseIdentifier: "showSug")
            self.sugTableView.registerClass(showSugTableViewCell.classForCoder(), forCellReuseIdentifier: "showSug")

            cell = showSugTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "showSug")
        }


        return cell!

However, the app now crashed on the return statement.

dpstart
  • 1,018
  • 1
  • 10
  • 25
  • by first telling us in what line the error occurred! – luk2302 Nov 15 '15 at 12:27
  • I said it crashed on the cell initialisation, which is `let cell = tableView.dequeueReusableCellWithIdentifier("showSug") as! showSugTableViewCell` – dpstart Nov 15 '15 at 12:29
  • well, to be exact: that is not the initialisation. Have you set up your custom cell in the IB to have the associated `showSug` identifier? Have you set the class of that cell to be `showSugTableViewCell`. Note: classes should start with upper case letters: `ShowSugTableViewCell` – luk2302 Nov 15 '15 at 12:31
  • Split the assignment in two operations: `let cellTemp = tableView.dequeueReusableCellWithIdentifier("showSug")` and `let cell = cellTemp as! showSugTableViewCel` and then debug to see what the type of `cellTemp` actually is. – luk2302 Nov 15 '15 at 12:31
  • Actually I did everything programatically, so I'm surely missing something to make it work... – dpstart Nov 15 '15 at 12:32
  • 1
    Yes, then you are missing something. Look at [this answer](http://stackoverflow.com/a/24752512/2442804) to see how to register your cell for a given identifier – luk2302 Nov 15 '15 at 12:34
  • categoryLbl.text! = "Text" .. try this – Rajan Maheshwari Nov 15 '15 at 12:36
  • I edited my post @luk2302 – dpstart Nov 15 '15 at 12:46
  • now you mixed up my two suggestions. remove my first suggestion splitting the assignment, implement only the linked solution and remove the nib-statement if you dont have a custom nib-file for the cell. – luk2302 Nov 15 '15 at 12:53
  • It worked, thank you! – dpstart Nov 15 '15 at 12:56
  • Possible duplicate of [Custom UITableViewCell register class in Swift](http://stackoverflow.com/questions/24751513/custom-uitableviewcell-register-class-in-swift) – luk2302 Nov 15 '15 at 18:57

1 Answers1

0

Try following case to confirm the issue - I strongly believe following 2 issues might be your problem

if let retCell = tableView.dequeueReusableCellWithIdentifier("showSug") {
    if let cell = retCell as? showSugTableViewCell {
        //use the cell object
    }else {
        //1. cell may not be of type showSugTableViewCell so filing t convert
//If it is the case check in identifier you set in nib
    }
}else {
    //2. tableView.dequeueReusableCellWithIdentifier may not be returning for "showSug"
//Check class type it return table cell
}
Girish Kolari
  • 2,515
  • 2
  • 24
  • 34