TableView在按钮操作后未更新

时间:2019-06-09 17:42:47

标签: ios swift xcode

我是swift的新手,我想尝试一个view,使其在加载时将在我的tableView上显示一些信息,然后在同一{ {1}}我有一个view和一个textfield

我希望button执行一个button,以便从我的服务器中获取数据并更新我的action

问题是该表未更新。如何获取要更新的表?

代码:

tableView

操作:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = 80;
    tableView.tableFooterView = UIView();
    self.url = URL(string: "http://xxxxxxxx.com/xxxxx/api/produtos/listagem/favoritos/\(userID)");
    downloadJson(_url: url!);

}


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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProdutoCell1") as? ProdutoCell else{
        return UITableViewCell()
    }

    cell.lbl_nome_json.text = produtos[indexPath.row].nome;
    cell.lbl_categoria_json.text = produtos[indexPath.row].categoria;
    cell.lbl_loja_json.text = produtos[indexPath.row].loja
    //= produtos[indexPath.row].categoria;
   // cell.lbl_loja.text = produtos[indexPath.row].loja;


    if let imageURL = URL(string: "http://xxxxxxxxxx.com/myslim/api/"+produtos[indexPath.row].url){

        DispatchQueue.global().async {
            let data = try? Data(contentsOf: imageURL);

            if let data = data{
                let image = UIImage(data: data);
                DispatchQueue.main.async {
                    cell.imgView_json.image = image;
                }
            }
        }



    }

    return cell;
}

编辑

我添加了一些@IBAction func btn_search(_ sender: Any) { if self.txt_search.text == "" { let alert = UIAlertController(title:"Alert",message: "Insira algo para pesquisar",preferredStyle: UIAlertController.Style.alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertAction.Style.default, handler: nil)) self.present(alert,animated: true,completion: nil) }else{ var pesquisa = String(); pesquisa = self.txt_search.text!; let url2 = URL(string: "http://xxxxxxxx.com/xxxxxx/api/produtos/listagem/favoritos/\(pesquisa)/\(self.userID)"); downloadJson(_url: url2!); } func downloadJson(_url: URL){ guard let downloadURL = url else {return} URLSession.shared.dataTask(with: downloadURL){data, urlResponse, error in guard let data = data, error == nil, urlResponse != nil else{ print("algo está mal"); return; } do{ let decoder = JSONDecoder(); let downloadedProdutos = try decoder.decode([ProdutoModel].self, from: data); self.produtos = downloadedProdutos print(downloadedProdutos); DispatchQueue.main.async { self.tableView.reloadData(); print("reload"); } }catch{ print("algo mal depois do download") } }.resume(); } ,以查看在print函数上的downloadedProdutos变量上返回了多少个对象。 在downloadJson(),我得到2个对象,这是正常的,因为我只有2个产品,但是完成viewdidload后,我仍然得到2个对象,尽管我应该只得到1个对象

1 个答案:

答案 0 :(得分:0)

您需要在tableView.dataSource = self中设置viewWillAppear,看起来您错过了func numberOfSections()-> Int方法。 像这样添加UITableViewDataSource class YourViewController: UIViewController, UITableViewDataSource,它将为您推荐所需的方法

相关问题