为什么我的自定义 UILabel 没有更新我的 Swift 应用程序中的正确数据?

发布于 2025-01-13 01:10:56 字数 3731 浏览 1 评论 0原文

我有一个 swift 小型 iOS 应用程序,它从公共 API 获取股票价格信息。它获取股票的最新价格并保存股票的先前价格。然后继续计算绝对价差为最新价格 - 先前价格和相对价差百分比为(绝对价格差/先前价格)* 100.0。但是,当我更新自定义 UILabels 以显示绝对和相对价格差异时,即使我可以在 UILabel 对象中看到最新价格更新的最新价格变化,我也会在 UILabel 对象的文本字段中打印 0.0。相对和绝对价格差异确实会上升和下降,分别变为绿色和红色,但有时它们会保持在 0.00% 和 (0.00),即使我可以看到最新的价格变化。我尝试从 cellForRowAt 委托方法打印出最新价格、绝对价格、相对价格和绝对价格,这证明正在计算正确的值,但我无法弄清楚为什么正确的值没有反映在 UILabels 中。据我所知,所有显示的值都尽可能精确到小数点后两位。

这是我的自定义表视图控制器类中的 cellForRowAt 方法;

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompanyStockCell", for: indexPath) as! CompanyStockCell
        
        cell.symbolValue.text = companyStockInfo[indexPath.row].symbol
        
        var latestPrice = companyStockInfo[indexPath.row].price
        latestPrice = round(latestPrice * 100.0) / 100.00
        cell.priceValue.text = String(format: "%.2f", arguments:[latestPrice])
        
        if let stockPrice = previousStockPrices[indexPath.row] {
            let previousPrice = round(stockPrice * 100.0) / 100.0
            var absoluteDifference = latestPrice - previousPrice
            absoluteDifference = round(absoluteDifference * 100.0) / 100.0
            absolutePriceDifferences[indexPath.row] = absoluteDifference
            
            var relativeDifference = 0.0
            if previousPrice != 0.00 {
                relativeDifference = (absoluteDifference / previousPrice) * 100.0
                relativeDifference = round(relativeDifference * 100) / 100.0
            }
            if indexPath.row == 0 {
                print(latestPrice, previousPrice, absoluteDifference)
            }
            
            if absoluteDifference > 0.0 {
                cell.priceDirectionImageView.image = UIImage(systemName: "arrowtriangle.up.fill")
                cell.priceDirectionImageView.tintColor = .systemGreen
                cell.relativePriceDiffValue.text = String(format: "%.2f%%", arguments: [relativeDifference])
                cell.relativePriceDiffValue.textColor = .systemGreen
                cell.absolutePriceDiffValue.text = String(format: "(%.2f)", arguments: [absoluteDifference])
                cell.absolutePriceDiffValue.textColor = .systemGreen
            } else if absoluteDifference < 0.0 {
                cell.priceDirectionImageView.image = UIImage(systemName: "arrowtriangle.down.fill")
                cell.priceDirectionImageView.tintColor = .systemRed
                cell.relativePriceDiffValue.text = String(format: "%.2f%%", arguments: [relativeDifference])
                cell.relativePriceDiffValue.textColor = .systemRed
                cell.absolutePriceDiffValue.text = String(format: "(%.2f)", arguments: [absoluteDifference])
                cell.absolutePriceDiffValue.textColor = .systemRed
            } else {
                cell.priceDirectionImageView.image = UIImage(systemName: "diamond.fill")
                cell.priceDirectionImageView.tintColor = .white
                cell.relativePriceDiffValue.text = "0.00%"
                cell.relativePriceDiffValue.textColor = .white
                cell.absolutePriceDiffValue.text = "(0.00)"
                cell.absolutePriceDiffValue.textColor = .white
            }
        } else {
            cell.priceDirectionImageView.image = UIImage()
            cell.priceDirectionImageView.tintColor = .white
            cell.relativePriceDiffValue.text = ""
            cell.absolutePriceDiffValue.text = ""
        }
        return cell
    }

这是用户界面的屏幕截图; 截图

I have a small iOS app in swift which fetches stock price information from a public API. It fetches the latest price for a stock and saves the previous price for the stock. It then goes on to calculate the absolute price difference as latest price - previous price and the relative price difference percentage as (absolute price difference / previous price) * 100.0. However, when I update the custom UILabels to show the absolute and relative price differences I get 0.0 printed in the UILabel objects' text fields even though I can see the latest price changing in the UILabel object for the latest price updates. The relative and absolute price difference do go up and down turning green and red respectively but occasionally they will stick at 0.00% and (0.00) even though I can see the latest price changing. I have tried printing out latest price, absolute price, relative price and absolute price from the cellForRowAt delegate method which proves the correct values are being calculated but I cannot figure out why the correct values are not reflected in the UILabels. All displayed values are calculated to two decimal places as best as I know how.

Here is my cellForRowAt method in my custom table view controller class;

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompanyStockCell", for: indexPath) as! CompanyStockCell
        
        cell.symbolValue.text = companyStockInfo[indexPath.row].symbol
        
        var latestPrice = companyStockInfo[indexPath.row].price
        latestPrice = round(latestPrice * 100.0) / 100.00
        cell.priceValue.text = String(format: "%.2f", arguments:[latestPrice])
        
        if let stockPrice = previousStockPrices[indexPath.row] {
            let previousPrice = round(stockPrice * 100.0) / 100.0
            var absoluteDifference = latestPrice - previousPrice
            absoluteDifference = round(absoluteDifference * 100.0) / 100.0
            absolutePriceDifferences[indexPath.row] = absoluteDifference
            
            var relativeDifference = 0.0
            if previousPrice != 0.00 {
                relativeDifference = (absoluteDifference / previousPrice) * 100.0
                relativeDifference = round(relativeDifference * 100) / 100.0
            }
            if indexPath.row == 0 {
                print(latestPrice, previousPrice, absoluteDifference)
            }
            
            if absoluteDifference > 0.0 {
                cell.priceDirectionImageView.image = UIImage(systemName: "arrowtriangle.up.fill")
                cell.priceDirectionImageView.tintColor = .systemGreen
                cell.relativePriceDiffValue.text = String(format: "%.2f%%", arguments: [relativeDifference])
                cell.relativePriceDiffValue.textColor = .systemGreen
                cell.absolutePriceDiffValue.text = String(format: "(%.2f)", arguments: [absoluteDifference])
                cell.absolutePriceDiffValue.textColor = .systemGreen
            } else if absoluteDifference < 0.0 {
                cell.priceDirectionImageView.image = UIImage(systemName: "arrowtriangle.down.fill")
                cell.priceDirectionImageView.tintColor = .systemRed
                cell.relativePriceDiffValue.text = String(format: "%.2f%%", arguments: [relativeDifference])
                cell.relativePriceDiffValue.textColor = .systemRed
                cell.absolutePriceDiffValue.text = String(format: "(%.2f)", arguments: [absoluteDifference])
                cell.absolutePriceDiffValue.textColor = .systemRed
            } else {
                cell.priceDirectionImageView.image = UIImage(systemName: "diamond.fill")
                cell.priceDirectionImageView.tintColor = .white
                cell.relativePriceDiffValue.text = "0.00%"
                cell.relativePriceDiffValue.textColor = .white
                cell.absolutePriceDiffValue.text = "(0.00)"
                cell.absolutePriceDiffValue.textColor = .white
            }
        } else {
            cell.priceDirectionImageView.image = UIImage()
            cell.priceDirectionImageView.tintColor = .white
            cell.relativePriceDiffValue.text = ""
            cell.absolutePriceDiffValue.text = ""
        }
        return cell
    }

Here is a screen shot of the UI;
screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文