JSON响应未在单元格内显示

问题描述 投票:-1回答:2

我正在创建一个Tableview,并尝试在单元格中包含一个通过API从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
    }
ios json uitableview swift3 cell
2个回答
1
投票

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

self.acoesTableView.reloadData()


0
投票

我做了一些研究和试用,并且可以找到一个非常简单(现在很明显)的解决方案,在收到请求结果后更新我的Label: - 我调用函数从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 searchCoption(active:String,for cell: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!)")
            }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.