0

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?

Dima
  • 23,484
  • 6
  • 56
  • 83
dvd.Void
  • 339
  • 1
  • 5
  • 21
  • please see https://stackoverflow.com/questions/16071503/how-to-tell-when-uitableview-has-completed-reloaddata – idelara Sep 12 '17 at 21:39
  • You should not manipulate your data array from inside `cellForRow(at:)`. The order in which this function is called is not defined, and it will be called multiple times as the table view scrolls. – Paulw11 Sep 12 '17 at 22:43

1 Answers1

2

Usually the best strategy is to avoid situations like this. You could call TNUArray.append at some other place - maybe when temperatureReadingArray contents are changed or when a toggleSwitch state is changed.

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) is intended to fill your cell, you shouldn't do any unrelated things there.

algrid
  • 5,600
  • 3
  • 34
  • 37