How do I set a custom cells text property to load in a table view?

I created a custom cell, created an instance of a struct, and set the text properties in the custom cell class.


import UIKit

class CustomTableViewCell: UITableViewCell {
    
    var my_Data = MyData()

    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
    }
    
    @IBOutlet weak var cellLoanName: UILabel!
    @IBOutlet weak var cellMonthlyPayment: UILabel!
    @IBOutlet weak var cellBalance: UILabel!
    
    @IBOutlet weak var cellDateAdded: UILabel!
    @IBOutlet weak var cellDueDate: UILabel!
    @IBOutlet weak var cellStatus: UILabel!
    
    func loadCell() {
        cellLoanName.text = my_Data.myLoanName
        cellMonthlyPayment.text = String(my_Data.myMonthlyPayment)
        cellBalance.text = String(my_Data.myBalance)
        cellDueDate.text = String(my_Data.myDueDate)
    }
}


When I push the 'confirm' button, I want data to be passed to a tuple that is outside of a class. I create an instance of the struct that will receive the data.


@IBAction func btnConfirm(_ sender: UIBarButtonItem) {
        var valueToPass = MyData()
        
        loanData()
        containsName()
        
        if txtLoanName.text == "" {
            return
        }
        if doesContainName == false {
            txtConfirmMessage.text = "Your New Loan\n'\(loanName)'\nhas been Added to your Loans!"
            txtConfirmMessage.textColor = .systemGreen
            
            loanData()
            
            valueToPass.myLoanName = loanName
            valueToPass.myDueDate = dueDate
            valueToPass.myMonthlyPayment = monthlyPayment
            valueToPass.myBalance = balance
        }
}


Then when I push a different button, the 'myLoans' button, I want the data to load in a table view showing the custom cell data. I created an instance of the custom cell class.


class MyLoans: UIViewController, UITableViewDataSource, UITableViewDelegate {

     var customCellData = CustomTableViewCell()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if stringLoansArray == [""] {
            let cell = myLoansTable.dequeueReusableCell(withIdentifier: "cell")
            cell?.textLabel?.text = stringEmptyArray[indexPath.row]
            cell?.textLabel?.font = .systemFont(ofSize: 30)
            cell?.textLabel?.textAlignment = .center
            cell?.textLabel?.numberOfLines = 2
            return cell!
        } else {
            let cell = myLoansTable.dequeueReusableCell(withIdentifier: "custom cell") as! CustomTableViewCell
            myLoan()
            customCellData.loadCell()
            
            cell.textLabel?.adjustsFontSizeToFitWidth = true
            return cell
        }
    }


When I run the program, it does everything right until I tap the 'My Loans' button. The program crashes and I get an error.


func loadCell() {
        cellLoanName.text = my_Data.myLoanName               Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
        cellMonthlyPayment.text = String(my_Data.myMonthlyPayment)
        cellBalance.text = String(my_Data.myBalance)
        cellDueDate.text

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value



Can I get some help please?

Replies

Could you explain in class MyLoans:


line 3 you create an instance of CustomTableViewCell, but not initialized, so cellLoanName is still nil.


So, you hve to change:

            let cell = myLoansTable.dequeueReusableCell(withIdentifier: "custom cell") as! CustomTableViewCell
            myLoan()
            customCellData.loadCell()
            cell.textLabel?.adjustsFontSizeToFitWidth = true
            return cell


into


            let cell = myLoansTable.dequeueReusableCell(withIdentifier: "custom cell") as! CustomTableViewCell
            myLoan()
            cell.loadCell()
            cell.textLabel?.adjustsFontSizeToFitWidth = true
            return cell

in that case, dont need customCellData


or change as


            customCellData = myLoansTable.dequeueReusableCell(withIdentifier: "custom cell") as! CustomTableViewCell
            myLoan()
            customCellDataloadCell()
            customCellData.textLabel?.adjustsFontSizeToFitWidth = true
            return customCellData

You should also show what myLoans() does. If it sets some properties of customCellData, then you should use the second solution:


There was a typo in this second proposal (. missing before loadCell())


            customCellData = myLoansTable.dequeueReusableCell(withIdentifier: "custom cell") as! CustomTableViewCell
            myLoan()
            customCellData.loadCell()
            customCellData.textLabel?.adjustsFontSizeToFitWidth = true
            return customCellData