am getting address label nil inUITableViewCell on calling tableviewreload function but after scrolling it appear

I am creating a table that has a prototype cell with an image & 3 labels but is getting an address label nil on calling tableviewreload function but after scrolling, it shows the address

My Tableview Code

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

        

        var device: Device!

        if self.searchOn{

            device = searchResult[indexPath.row]

        }else{

            switch self.stage {

            case 0:

                if devices?[indexPath.row].address == "" {

                    print("\(devices?[indexPath.row].name ?? "") Address Blank :- \(devices?[indexPath.row].address ?? "")")

                }

                device = devices?[indexPath.row];

                break;

            case 1:

                device = movingDevices[indexPath.row];

                break;

            case 2:

                device = stoppedDevices[indexPath.row];

                break;

            case 3:

                device = offlineDevices[indexPath.row];

                break;

            default:

                break;

            }

        }

        

        if isDeviceExpired(device){

            let cell = tableView.dequeueReusableCell(withIdentifier: "deviceRenewCell", for: indexPath) as! DeviceRenewCell;

            cell.device = device

            cell.renewBlock = { [weak self] device in

                if let vc = self?.storyboard?.instantiateViewController(withIdentifier: "DeviceRenewalViewController") as? DeviceRenewalViewController{

                    vc.preSelectedDevice = device

                    self?.navigationController?.pushViewController(vc, animated: true)

                }

            }

            return cell

        }else{

            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell;

            if device.address == "" {

                print("\(device.name ?? "") Address Blank :- \(device.address ?? "")")

            }

            cell.device = device

            return cell

        }

        

    }

My TableviewCell Code

class DeviceTableViewCell: UITableViewCell {

    public var device : Device?{

        didSet{

            self.nameLabel.text = self.device?.name;

            self.statusLabel.text = self.device?.status;

            let twelevehrsformat = DateUtil.format(self.device?.dtTracker ?? "", fromFormat: "yyyy-MM-dd HH:mm:ss", toFormat: "dd-MMM-yy hh:mm:ss a", tf: false)

            let twentyfourhrsformat = DateUtil.format(self.device?.dtTracker ?? "", fromFormat: "yyyy-MM-dd HH:mm:ss", toFormat: "dd-MMM-yy HH:mm:ss", tf: true)

            if UserDefaults.standard.bool(forKey: "hrsFormat") == true {

                self.timeLabel.text = twentyfourhrsformat

            } else {

                self.timeLabel.text = twelevehrsformat

            }

            if self.device?.address == "" {

                print("\(self.device?.name) Address blank :- \(self.device?.address ?? "")")

            } else {

                self.addressLabel.text = self.device?.address;

            }

            

            self.speedLabel.text = self.device?.speed;

            self.speedUnitLabel.text = self.device?.unitOfSpeed;

            if let icon = self.device?.icon{

                self.vehicleImage.yy_setImage(with: URL(string: "\(Configuration.getBaseUrl())\(icon)"), options: [.ignoreFailedURL])

            }

            if let conn = self.device?.connectionImg{

                self.connectionImage.yy_setImage(with: URL(string: "\(Configuration.getBaseUrl())\(conn)"), options: [.ignoreFailedURL])

            }

            if let ig = self.device?.ignition{

                if ig == 1{

                    self.stateImage.image = UIImage(named: "ignition_on");

                }else{

                    self.stateImage.image = UIImage(named: "ignition_off");

                }

            }else{

                self.stateImage.image = UIImage(named: "ignition_na");

            }



        }

    }

    

    @IBOutlet weak var nameLabel: UILabel!;

    @IBOutlet weak var statusLabel: UILabel!;

    @IBOutlet weak var timeLabel: UILabel!;

    @IBOutlet weak var addressLabel: UILabel!;

    @IBOutlet weak var speedLabel: UILabel!;

    @IBOutlet weak var speedUnitLabel: UILabel!;

    @IBOutlet weak var vehicleImage: UIImageView!;

    @IBOutlet weak var connectionImage: UIImageView!;

    @IBOutlet weak var stateImage: UIImageView!;

    @IBOutlet weak var bed: UIView!;

    

    override func awakeFromNib() {

        super.awakeFromNib()

        self.bed.layer.masksToBounds = true;

        self.bed.layer.cornerRadius = 3.0;

        

    }

    

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

        super.setSelected(selected, animated: animated)

        

        // Configure the view for the selected state

    }

    

}

**Before Scrolling **

**After Scrolling **

Your addressLabel is not nil (app would crash), it is empty.

Do you get a print from this:

            if self.device?.address == ""  {
                print("\(self.device?.name) Address blank :- \(self.device?.address ?? "")")
            } else {
                self.addressLabel.text = self.device?.address;
            }

What is the dash ("-") you get in the first case ?

WHere and how do you load devices ? It could be that devices are not yet loaded when you first display, but have had time to load at second display.

It is pretty hard to grasp the code with all the extra lines. You should use paste and Match Style.

Note : you don't need break at the end of a case statement not ; at the end of each statement (as is the case in objective C).

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

        var device: Device!

        if self.searchOn{
            device = searchResult[indexPath.row]
        } else {
            switch self.stage {

            case 0:
                if devices?[indexPath.row].address == "" {
                    print("\(devices?[indexPath.row].name ?? "") Address Blank :- \(devices?[indexPath.row].address ?? "")")
                }
                device = devices?[indexPath.row];
                break;  // <<-- useless

            case 1:
                device = movingDevices[indexPath.row];
                break;  // <<-- useless
                
            case 2:
                device = stoppedDevices[indexPath.row];
                break;  // <<-- useless

            case 3:
                device = offlineDevices[indexPath.row];
                break;  // <<-- useless

            default:
                break;

            }

        }

        if isDeviceExpired(device){

            let cell = tableView.dequeueReusableCell(withIdentifier: "deviceRenewCell", for: indexPath) as! DeviceRenewCell;

            cell.device = device

            cell.renewBlock = { [weak self] device in
                if let vc = self?.storyboard?.instantiateViewController(withIdentifier: "DeviceRenewalViewController") as? DeviceRenewalViewController{
                    vc.preSelectedDevice = device
                    self?.navigationController?.pushViewController(vc, animated: true)

                }

            }

            return cell

        } else {

            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell;
            if device.address == "" {
                print("\(device.name ?? "") Address Blank :- \(device.address ?? "")")
            }
            cell.device = device

            return cell

        }

    }

class DeviceTableViewCell: UITableViewCell {

    public var device : Device?{

        didSet{

            self.nameLabel.text = self.device?.name;
            self.statusLabel.text = self.device?.status;
            let twelevehrsformat = DateUtil.format(self.device?.dtTracker ?? "", fromFormat: "yyyy-MM-dd HH:mm:ss", toFormat: "dd-MMM-yy hh:mm:ss a", tf: false)
            let twentyfourhrsformat = DateUtil.format(self.device?.dtTracker ?? "", fromFormat: "yyyy-MM-dd HH:mm:ss", toFormat: "dd-MMM-yy HH:mm:ss", tf: true)

            if UserDefaults.standard.bool(forKey: "hrsFormat") == true {
                self.timeLabel.text = twentyfourhrsformat
            } else {
                self.timeLabel.text = twelevehrsformat
            }

            if self.device?.address == "" {
                print("\(self.device?.name) Address blank :- \(self.device?.address ?? "")")
            } else {
                self.addressLabel.text = self.device?.address;
            }

            self.speedLabel.text = self.device?.speed;
            self.speedUnitLabel.text = self.device?.unitOfSpeed;

            if let icon = self.device?.icon {
                self.vehicleImage.yy_setImage(with: URL(string: "\(Configuration.getBaseUrl())\(icon)"), options: [.ignoreFailedURL])
            }

            if let conn = self.device?.connectionImg{
                self.connectionImage.yy_setImage(with: URL(string: "\(Configuration.getBaseUrl())\(conn)"), options: [.ignoreFailedURL])
            }

            if let ig = self.device?.ignition {
                if ig == 1 {
                    self.stateImage.image = UIImage(named: "ignition_on");
                } else {
                    self.stateImage.image = UIImage(named: "ignition_off");
                }

            } else {
                self.stateImage.image = UIImage(named: "ignition_na");
            }

        }
    }

    

    @IBOutlet weak var nameLabel: UILabel!;

    @IBOutlet weak var statusLabel: UILabel!;

    @IBOutlet weak var timeLabel: UILabel!;

    @IBOutlet weak var addressLabel: UILabel!;

    @IBOutlet weak var speedLabel: UILabel!;

    @IBOutlet weak var speedUnitLabel: UILabel!;

    @IBOutlet weak var vehicleImage: UIImageView!;

    @IBOutlet weak var connectionImage: UIImageView!;

    @IBOutlet weak var stateImage: UIImageView!;

    @IBOutlet weak var bed: UIView!;

    
    override func awakeFromNib() {

        super.awakeFromNib()

        self.bed.layer.masksToBounds = true;
        self.bed.layer.cornerRadius = 3.0;

    }

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

        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

@Claude31 **Code File Link **

https://drive.google.com/file/d/1N8CXPHBsElZ2r2hGjUM5LQ5nLsL5TOzQ/view?usp=sharing

am getting address label nil inUITableViewCell on calling tableviewreload function but after scrolling it appear
 
 
Q