Martin Perry
Martin Perry

Reputation: 9527

Create programatically UITableViewController with user defined cell

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.

Upvotes: 0

Views: 69

Answers (4)

Shaheen M Basheer
Shaheen M Basheer

Reputation: 1010

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.

Upvotes: 0

Scott Thompson
Scott Thompson

Reputation: 23701

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?

Upvotes: 0

Mike Taverne
Mike Taverne

Reputation: 9352

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.

Upvotes: 1

Balaji Galave
Balaji Galave

Reputation: 1076

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

Upvotes: 5

Related Questions