Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体

0
votes
answers
楼主
39 views
+10

未在單元格內顯示JSON響應

-1

我正在創建Tableview並嘗試在單元格中包含一個通過API從JSON接收的信息。 信息(JSON)收到的很好,並在變量內正確記錄。 但是,我可以發現的是,由於收到的信息帶有一個小的「延遲」,因此不會在單元格創建時刻將單元格的標籤文本設置爲默認變量內容。 我想解決的辦法是在解析JSON內容的時刻更新標籤,對吧?我該怎麼做呢? (更新單元格的標籤後,它已被創建) 任何其他洞察力/解決方案非常感謝。未在單元格內顯示JSON響應

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, AddStock { 

    let operacoesAcoes = ListaOperacoes() 
    var todasAsOperacoes : [Operacao] = [] 

    @IBOutlet weak var acoesTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     acoesTableView.delegate = self 
     acoesTableView.dataSource = self 
     acoesTableView.register(UINib(nibName: "StandardStockListCell", bundle: nil), forCellReuseIdentifier: "standardCell") 
     operacoesAcoes.consolidaAcoes() 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return operacoesAcoes.carteiraAcoes.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "standardCell", for: indexPath) as! StandardStockListCell 
     let informacaoCompletaAcao = operacoesAcoes.carteiraAcoes[indexPath.row] 

     cell.codigoTextField.text = informacaoCompletaAcao.codigoAcao 
     cell.nomeTextField.text = informacaoCompletaAcao.nomeAcao 
     cell.quantidadeTotal.text = String(informacaoCompletaAcao.quantidadeTotal) 
     cell.precoMedioLabel.text = String(format: "%.2f", informacaoCompletaAcao.precoMedio) 

     // 
     // This is the part of the code that should set one label with a value returned from "buscaCotacao" but it does not work 
     // because at the time the cell is displayed it is still not updated from JSON information: 
     // Note: the buscaCotacao func is working fine 

     cell.precoAtualLabel.text = buscaCotacao(ativo: informacaoCompletaAcao.codigoAcao) 

     return cell 
    } 
沙发
0
1

您需要在接收並解析JSON之後重新加載主線程上的表視圖。

self.acoesTableView.reloadData()

板凳
0
0

我做了一些研究和選拔賽,並在接收到請求的結果後能想出一個非常簡單的(和現在很明顯)解決方案來更新我的標籤: - 我把它檢索功能來自API的更新信息的信息(「buscaCotacao」),包括[cell row]信息 - 我從函數內部更新單元格的標籤,僅在收到回覆後纔會發生:

func tableView (_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "standardCell", for: indexPath) as! StandardStockListCell 
    let informacaoCompletaAcao = operacoesAcoes.carteiraAcoes[indexPath.row] 

    cell.codigoTextField.text = informacaoCompletaAcao.codigoAcao 
    cell.nomeTextField.text = informacaoCompletaAcao.nomeAcao 
    cell.quantidadeTotal.text = String(informacaoCompletaAcao.quantidadeTotal) 
    cell.precoMedioLabel.text = "R$ "+String(format: "%.2f", informacaoCompletaAcao.precoMedio) 

    buscaCotacao(ativo: informacaoCompletaAcao.codigoAcao, for: cell, indexPath: indexPath) 

    return cell 
} 

而在功能:

FUNC buscaCotacao(ativo:字符串,用於細胞:StandardStockListCell,indexPath:IndexPath){

let finalURL = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&apikey=XXXXXXXXXXXXXX&outputsize=compact&symbol=" + ativo 

    Alamofire.request(finalURL, method: .get) 
     .responseJSON { response in 
      if response.result.isSuccess { 
       let resultadoJSON : JSON = JSON(response.result.value!) 
       let resultado = Float (self.parseResultado(json: resultadoJSON))! 
       cell.precoAtualLabel.text = "R$ "+String(format: "%.2f", resultado) 
       self.cotacoes[ativo] = resultado 

      } else { 
       cell.precoAtualLabel.text = "N/D" 
       print("Error: (response.result.error!)") 
      } 
    } 
} 
discard