uitableview custom cell has nil values

Hello,


Sorry i placed it in the wrong forum. the way this ui works is still unfamiliar to me if I don't know the valid forums that are available. I know to put it in swift next time as this is a swift programming language issue.


I've been learning how to do tableviews.


I have implemented a custom tableviewcell on a view which is part of my main screen and it's controller.


I've implemented methods for the datasource and delegate


this is the main code of the datasource function.


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! PanicAlertTableViewCell
        let sender = sender_list[indexPath.row]
        let number = indexPath.row
        let count = String.localizedStringWithFormat("%I)", number)
           print(sender_list[number])
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        if sender.is_sound == "0" {
            cell.btnOff.isHidden = true
        }
        cell.delegate = self
        cell.set(number: count, alert: sender.name, indexPath: indexPath)
        return cell

    }

these are the other 2 functions used with the tableview


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        NSLog("Counting rows")
        return sender_list.count
  
    }


func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


Here is the class in question:

class PanicAlertTableViewCell: UITableViewCell {
   
    public var delegate : PanicAlertTableViewCellDelegate?
   


    var indexPath: IndexPath?




    @IBOutlet weak var lblNumber: UILabel!
   
    @IBOutlet weak var lblAlert: UILabel!
    @IBOutlet weak var btnOff: UIButton!
   
    func set(number: String, alert: String, indexPath: IndexPath) {
        self.lblNumber.text = number
        self.lblAlert.text = alert
        self.indexPath = indexPath
    }
   
    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
    }


   
   
    @IBAction func btnOff(_ sender: UIButton) {
        if (indexPath != nil && delegate != nil) {
            delegate!.panicButtonClicked(cell: self, indexPath: indexPath!)
        }
       
       
    }
}



sender_list is an array of PanicSender objects however the number items in this array are always changing, so i run the reloadData method on the tableview.


however when it goes to set the number and name with the labels in the cell it crashes with BAD_EXC_INSTRUCTION


I learnt that the cell itself has to have an identifier. this identifier is PanicIdentifier.

however it doesn't load the awakeFromNib function (I have a breakpoint on the function but the program never stops)

All three components are connected with the storyboard.


I imagine i'm just missing something as this is my first time using tableviews.


It appears as though there are some solutions but feel like i'm following a rabbit hole. I've looked at these articles:

https://stackoverflow.com/questions/20725151/how-to-properly-init-a-custom-uitableviewcell

https://stackoverflow.com/questions/29321383/iboutlet-is-nil-but-it-is-connected-in-storyboard-swift




Thanks for any help in advance!

Please format the code to ease referncing


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! PanicAlertTableViewCell
        let sender = sender_list[indexPath.row]
        let number = indexPath.row
        let count = String.localizedStringWithFormat("%I)", number)
           print(sender_list[number])
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        if sender.is_sound == "0" {
            cell.btnOff.isHidden = true
        }
        cell.delegate = self
        cell.set(number: count, alert: sender.name, indexPath: indexPath)
        return cell

    }

// these are the other 2 functions used with the tableview

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        NSLog("Counting rows")
        return sender_list.count

    }

func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


when it goes to set the number and name with the labels in the cell it crashes with BAD_EXC_INSTRUCTION

Where does the crash occurs (which line ?) Is it line 12 ?


If so, can you show your set func ?


Note : you don't have to set delegate on line 11. You just want to return a cell content.

Simply delete this line.


line 8 : I don't know what is_sound is.


Is it 0 / 1 for false / true ?

In such a cae, you should declare as Bool and test

if sender.is_sound {

Hi Claude.


Thanks for replying.


Okay.


I'll edit the original post. i'll include the class that has the problem in question.


In reply to your question:


Delegate method is not referring to the cell but a custom delegate. It doesn't have a problem with that. the problem is with IBOutlets.


I'll be changing that is_sound to boolean eventually, just trying to get to work. it is a string whose values are "0" and "1" meaning if it is to warn with a sound (1) or not (0)

The problems vanished after adding this line to the tableView cellForRowAtIndexPath method:


if let dequeuedcell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? PanicAlertTableViewCell {
            cell = dequeuedcell
        } else {
            cell = PanicAlertTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
        }



I'll say that this problem is resolved. Thanks. There seems to be a new problem that's come up of not displaying anything on the tableview when the view that this table is part of is set to visible

Exact.


using as! was a bit risky. What you do is the usual pattern.


What is surprising, because in some cases as? downcast fails (then you get nil), is that

let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! PanicAlertTableViewCell

did not cause crash.


But you have found the correction, that's most important.


Note: did you register the class, with a call to


tableView.registerClass(CellClass.self, forCellReuseIdentifier: "CellID")

uitableview custom cell has nil values
 
 
Q