I'm having an issue with reloading a tableView, Here's the batch of code I have:
func updateIncomingData () {
print ("Received String:")
print (receivedDataString)
print("Clearing TNU Array")
TNUArray.removeAll()
temperatureReadingArray = receivedDataString.components(separatedBy: " ")
print("temperatureReadingArray is:")
print(temperatureReadingArray)
self.temperatureReadingsTable.reloadData()
calculateTNU()
}
func calculateTNU()
{
var TNU: Double = 0.000
print("TNU Array:")
print(TNUArray)
let minValue = TNUArray.min()
print("Values MIN/MAX are:")
print(minValue ?? "nil")
let maxValue = TNUArray.max()
print(maxValue ?? "nil")
if (minValue != nil && maxValue != nil)
{
TNU = maxValue! - minValue!
calculatedTNU = String(format:"%.3f", TNU)
TNULabel.text = calculatedTNU
}
else
{
print("Max or Min had nil values")
}
}
Now, here you can see I call reloadData() before calling out calculateTNU(). This calls up the table load functions, specially interested in this one:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: temperatureReading) as! TemperatureReading
cell.channelLabel.text = channelNames[indexPath.row]
cell.readingLabel.text = temperatureReadingArray[indexPath.row]
if (channelNames.count == 1)
{
cell.toggleSwitch.isHidden = true
}
if (cell.toggleSwitch.isOn)
{
if let value = Double(cell.readingLabel.text!)
{
print("valid number, appending")
TNUArray.append(value)
}
else
{
print("Not a valid number reading")
}
}
return cell
}
Now, the problem is that it runs calculateTNU() before it finishes reloading the data, and this causes my calculateTNU() function to not have any values to read from (since when the table is populating it also fills the array needed for the TNU calculation).
Is there a "wait till it reloads" before executing the next command function?