0

I have created UITableViewController programatically in viewDidLoad():

resultsController = UITableViewController(style: UITableViewStyle.plain)
resultsController.tableView.register(MyTableCellTableViewCell.self, forCellReuseIdentifier: "userFoundCell")
resultsController.tableView.dataSource = self
resultsController.tableView.delegate = self

However, when I later do

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   var cell: MyTableCellTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "userFoundCell", for: indexPath) as? MyTableCellTableViewCell

   if (cell == nil){
        cell = MyTableCellTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "userFoundCell")
   }

   cell!.lblTmp!.text = "test"
   return cell!
}

cell is never nil, I have tested it

Code crash with:

fatal error: unexpectedly found nil while unwrapping an Optional value on cell!.lblTmp!.text = "test"

My MyTableCellTableViewCell looks like this

class MyTableCellTableViewCell: UITableViewCell {
    @IBOutlet var lblTmp: UILabel!

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

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

IBoutlet is connected in Interface Builder to GUI elements. The same class is used on another place as well.

6
  • @Sulthan it is connected. Or at least in my second table, that is using the same class, but is inited from IB, all is working Commented May 6, 2017 at 18:42
  • You said the label is connected but you also said you have no xib file. Since you are not using storyboard either, how exactly are you defining the view for MyTableCellTableViewCell and how exactly are you connecting the label?
    – Sulthan
    Commented May 6, 2017 at 18:45
  • @Sulthan I have a storyboard Commented May 6, 2017 at 18:46
  • You are not using a storyboard though. Your resultsController is not instantiated from a storyboard.
    – Sulthan
    Commented May 6, 2017 at 18:47
  • @Sulthan I have combination of both. Something from storyboard, something from code. This is inited from code, on another place from storyboard Commented May 6, 2017 at 18:49

4 Answers 4

5

If you have your custom tableview cell with xib and class then you should register your cell by following way.

If you have your table view cell xib file name like "MyTableCellTableViewCell" this then replace your line

resultsController.tableView.register(MyTableCellTableViewCell.self, forCellReuseIdentifier: "userFoundCell")

with this line

resultsController.tableView.register(UINib(nibName: "MyTableCellTableViewCell", bundle: nil), forCellReuseIdentifier: "userFoundCell")
5
  • I dont have xib file. Commented May 6, 2017 at 18:43
  • then how did you make @IBOutlet var lblTmp: UILabel! this line into Custom table view cell.? Commented May 6, 2017 at 18:48
  • I have it connected to storyboard Commented May 6, 2017 at 18:49
  • then its a prototype cell. Commented May 6, 2017 at 18:50
  • resultsController would have to be instantiated from that storyboard and from that specific controller which contains the prototype cell.
    – Sulthan
    Commented May 6, 2017 at 18:56
1

This pattern of creating cells is wrong:

if (cell == nil){
    cell = MyTableCellTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "userFoundCell")
}

You should do this instead (force cast using ! is appropriate here in my opinion):

let cell = tableView.dequeueReusableCell(withIdentifier: "userFoundCell", for: indexPath) as! MyTableCellTableViewCell

If this line crashes, then it means your custom cell was not registered properly. This will help you to isolate the problem.

0

While the cell is not nil, you don't seem to be checking whether or not lblTmp is nil. If lblTmp is nil then trying to set cell!.lblTmp!.text = "test" will crash with unexpectedly found nil while unwrapping an Optional value. How are you setting lblTmp to a value?

0

You dont have to do unwrapping cell!.lblTmp!.text = "test" after declaring an implicitily unwrapped optional @IBOutlet var lblTmp: UILabel!

Use cell!.lblTmp.text = "test" instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.