我有一个tableView控制器,其中每一行将代表特定加密货币的值。我为用户要跟踪的每种不同的加密货币提供了一个类,称为CryptoCurrency
。在TableView CellforRowAt
函数下,我调用了另一个名为getCryptoData的函数,该函数将使用AlamoFire发送api请求以获取每种加密货币的价格。
问题在于cellForRowAt
函数在getCryptoData函数可以完成并更新模型之前返回默认价格为0.00的单元格。
我假设这是因为该函数异步运行?
如何使它在返回单元格之前等待函数完成,还是在完成后重新加载单元格?
我尝试在tableView.reloaddata()
函数的末尾添加updateCryptoData
,但这导致了无限循环。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CryptoCurrencyCell", for: indexPath)
let cryptoCurrencyItem = cryptoCurrencyContainer.listOfCryptoCurrencies[indexPath.row]
getCryptoData(url: getApiString(for: cryptoCurrencyItem.id), currencyItem: cryptoCurrencyItem)
cell.textLabel?.text = cryptoCurrencyContainer.listOfCryptoCurrencies[indexPath.row].name + "\(cryptoCurrencyItem.currentPrice)"
print(cryptoCurrencyItem.currentPrice)
return cell
}
func getCryptoData(url: String, currencyItem: CryptoCurrency) {
Alamofire.request(url, method: .get).responseJSON {
response in
if response.result.isSuccess {
print("Sucess! bitcoin data")
let cryptoDataJSON : JSON = JSON(response.result.value!)
print(cryptoDataJSON)
self.updateCryptoData(json: cryptoDataJSON, currencyItem: currencyItem)
} else {
print("oops")
print("Error: \(String(describing: response.result.error))")
//self.bitcoinPriceLabel.text = "Connection Issues"
}
}
}
func updateCryptoData(json : JSON, currencyItem: CryptoCurrency) {
if let cryptoPriceResult = json["ask"].double {
//bitcoinPriceLabel.text = String(bitcoinResult)
currencyItem.updatePrice(price: cryptoPriceResult)
print(currencyItem.currentPrice)
} else {
//bitcoinPriceLabel.text = "Price Unavailable"
print("something aint right")
}
}
cellForRowAt
函数下有一个打印语句:print(cryptoCurrencyItem.currentPrice)
在单元返回之前捕获当前价格。控制台显示仍为0.00,表示getCryptoData函数尚未完成运行。
最佳答案
也许这不是最好的解决方案,但是您可以将单元格传递给您的方法
func getCryptoData(url: String, currencyItem: CryptoCurrency, for cell: UITableViewCell) {
Alamofire.request(url, method: .get).responseJSON {
response in
if response.result.isSuccess {
print("Sucess! bitcoin data")
let cryptoDataJSON : JSON = JSON(response.result.value!)
print(cryptoDataJSON)
self.updateCryptoData(json: cryptoDataJSON, currencyItem: currencyItem, for: cell)
} else {
print("oops")
print("Error: \(String(describing: response.result.error))")
//self.bitcoinPriceLabel.text = "Connection Issues"
}
}
}
func updateCryptoData(json : JSON, currencyItem: CryptoCurrency, for cell: UITableViewCell) {
if let cryptoPriceResult = json["ask"].double {
//bitcoinPriceLabel.text = String(bitcoinResult)
currencyItem.updatePrice(price: cryptoPriceResult)
print(currencyItem.currentPrice)
cell.textLabel?.text = "\(cryptoPriceResult)" // your cell
} else {
//bitcoinPriceLabel.text = "Price Unavailable"
cell.textLabel?.text = "\(cryptoPriceResult)" // your cell
print("something aint right")
}
}
由于
UITableViewCell
是引用类型,因此异步调用完成后,它将更新单元格文本标签。关于ios - 在API请求可以更新模型之前,TableView加载单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57438492/