簡體   English   中英

按鈕操作后TableView不會更新

[英]TableView not updating after button action

我對swift相對比較新,而且我試圖有一個view ,當它加載時會在我的tableView上顯示一些信息,然后在同一個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();
}

編輯

我在downloadJson()函數中添加了一些print以查看我的downloadedProdutos變量返回了多少個對象。 viewdidload我得到2個對象,它正常,因為我只有2個產品,但是當action完成后我仍然得到2個對象,雖然我應該只得到1個對象

你需要在viewWillAppear設置tableView.dataSource = self ,它看起來你錯過了func numberOfSections() - > Int方法。 添加UITableViewDataSource就像這個class YourViewController: UIViewController, UITableViewDataSource ,它會推薦你需要的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM