为什么我的自定义 UILabel 没有更新我的 Swift 应用程序中的正确数据?
我有一个 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论