ios - 重新加载 UITableView 并在之后运行一些东西

标签 ios swift uitableview

<分区>

我在重新加载 tableView 时遇到问题,这是我拥有的那批代码:

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")
        }  
    }

现在,在这里您可以看到我在调用 calculateTNU() 之前调用了 reloadData()。这调用了表加载函数,对这个函数特别感兴趣:

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
    }

现在,问题是它在完成重新加载数据之前运行 calculateTNU(),这导致我的 calculateTNU() 函数没有任何可读取的值(因为当表格正在填充时,它也会填充所需的数组用于 TNU 计算)。

在执行下一个命令功能之前是否有“等待重新加载”?

最佳答案

通常最好的策略是避免这种情况。您可以在其他地方调用 TNUArray.append - 可能是在 temperatureReadingArray 内容更改或 toggleSwitch 状态更改时。

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 旨在填充您的单元格,您不应该在那里做任何无关的事情。

关于ios - 重新加载 UITableView 并在之后运行一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46185664/

相关文章:

ios - 如何对齐 UICollectionViewCell?

swift - 为什么是 'there cannot be more than one conformance, even with different conditional bounds' ?

ios - 获取单元格的 indexPath.row

ios - 调用在 UITableview 中显示图像时图像 url 显示错误

ios - UIBarButtonItem 出现在 iPad 模拟器中,但不在实际应用中

ios - Xcode 6 在 IB 中添加 tableHeaderView

ios - 当应用程序处于后台时,推送通知不起作用

ios - 如何从Apple Push Notifications获取userInfo

swift - 绑定(bind)到 NSToolbarItem 的 Cocoa 连接是否会阻止取消初始化?

ios - AFNetworking 3.0 GET请求保存到iOS中的类中