2017-11-18 104 views
-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 
    } 

回答

1

您需要在接收并解析JSON之后重新加载主线程上的表视图。

self.acoesTableView.reloadData()

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