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 **