值不会显示到多个选择器视图中

问题描述 投票:0回答:1

我正在制作一个带有viewcontroller的应用程序,该应用程序在两个不同的pickerview中显示两个数组,一个数组具有保存在应用程序中的值,其他数据通过来自其余服务器的其余调用来检索。我试图打印其余服务器的值并显示它们但是它们没有显示在uipickerview中我该如何解决?什么是应得的

class ModificaArticoloMagazzino: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate,UIPickerViewDelegate, UIPickerViewDataSource
{

    private var TipologiaUIPicker: UIPickerView = UIPickerView()
    private var MagazzinoModUIPicker: UIPickerView = UIPickerView()
    private let valori = ["Carico","Scarico","Rimozione","Spostamento"]
    private var SetValue:String="Carico"
    private var filteredDataMagazzini: [MagazzinoStruct] = []

    init() {
        super.init(nibName: nil, bundle: nil)
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        hideKeyboardWhenTappedAround()
        self.view.backgroundColor = UIColor.white


        //Caricamento Magazzini
       //Caricamento Magazzini
    DispatchQueue.main.async {
        let mtemp = Magazzino(User: self.u)
        mtemp.CaricaMagazzini(completion: { result in

                self.filteredDataMagazzini=result
                print("\n \n Magazzini caricati: \(result.count)")
            //Configurazione Picker View Tipologia Preventivo
            self.labelMagazzinoMod = UILabel(frame: CGRect(x: 20, y: 400, width: 120, height: 21))
            self.labelMagazzinoMod.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
            self.labelMagazzinoMod.text = "Magazzino: "
            self.view.addSubview(self.labelMagazzinoMod)
            self.MagazzinoModUIPicker = UIPickerView(frame: CGRect(x: 150, y: 400, width: 200, height: 50))
            self.MagazzinoModUIPicker.delegate = self
            self.MagazzinoModUIPicker.dataSource = self
            self.MagazzinoModUIPicker.backgroundColor = UIColor.white
            self.view.addSubview(self.MagazzinoModUIPicker)

        });
    }

        //Configurazione Picker View Tipologia Preventivo
        labelTipologia = UILabel(frame: CGRect(x: 20, y: 270, width: 120, height: 21))
        labelTipologia.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
        labelTipologia.text = "Tipologia: "
        self.view.addSubview(labelTipologia)
        self.TipologiaUIPicker = UIPickerView(frame: CGRect(x: (self.view.frame.width / 2) - 70, y: 270, width: 200, height: 50))
        self.TipologiaUIPicker.delegate = self as UIPickerViewDelegate
        self.TipologiaUIPicker.dataSource = self as UIPickerViewDataSource
        self.TipologiaUIPicker.backgroundColor = UIColor.white
        self.view.addSubview(TipologiaUIPicker)
        //Quantita--> labelQuantitaMod e txtQuantita
        labelQuantitaMod = UILabel(frame: CGRect(x: 20, y: 220, width: 60, height: 21))
        labelQuantitaMod.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
        labelQuantitaMod.text = "Quantita: "
        self.view.addSubview(labelQuantitaMod)
        txtQuantita = UITextField(frame: CGRect(x: 120, y: 220, width: 200, height: 21));
        txtQuantita.backgroundColor = UIColor.lightGray
        txtQuantita.text = "1"
        self.view.addSubview(txtQuantita)


    }

    //Funzione per aggiornare articolo in magazzino
    @objc func AggiornaArticoloMagazzino(sender: UIButton!) {
         let mtemp = Magazzino(SetArticolo: art, User: u)

        if(SetValue=="Carico" || SetValue=="Scarico" || SetValue=="Rimozione"){

            mtemp.Aggiorna(Quantita: Int(txtQuantita.text!)!, QuantitaPrecedente: art.Quantita!, IdMagazzino2: 0,Modalita:SetValue, completion: { result in

                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
                    if(result==false){
                        let alertController = UIAlertController(title: "Errore", message: "\(self.SetValue) non riuscito ", preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else{
                        if(self.SetValue=="Rimozione"){
                            self.previosScreen()
                        }
                        else if(self.SetValue=="Scarico"){
                            self.art.Quantita=self.art.Quantita!-Int(self.txtQuantita.text!)!
                        }
                        else if(self.SetValue=="Carico"){
                            self.art.Quantita=self.art.Quantita!+Int(self.txtQuantita.text!)!
                        }
                        self.labelQuantita.text = "Quantita attuale: \(String(describing: self.art.Quantita!))"
                    }
                 });
            });
        }
        else{

            mtemp.Aggiorna(Quantita: Int(txtQuantita.text!)!, QuantitaPrecedente: art.Quantita!, IdMagazzino2: 0,Modalita:SetValue, completion: { result in

                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
                    if(result==false){
                        let alertController = UIAlertController(title: "Errore", message: "\(self.SetValue) non riuscito ", preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                 });
            });
        }

    }

    //Questa funzione verifica quale selezione viene effettuata
    func CheckSelezione(row: Int){

        if(valori[row]=="Carico"){

            txtQuantita.isEnabled = true
            txtQuantita.isUserInteractionEnabled = true
        }
        else if(valori[row]=="Scarico"){
            txtQuantita.isEnabled = true
            txtQuantita.isUserInteractionEnabled = true
        }

        else if(valori[row]=="Rimozione"){
            txtQuantita.isEnabled = false
            txtQuantita.isUserInteractionEnabled = false
        }
        else{
            txtQuantita.isEnabled = true
            txtQuantita.isUserInteractionEnabled = true

        }

        SetValue=valori[row]
    }



    //Funzione per tornare alla schermata principale

    func previosScreen(){
        let returnView = TabBarViewController()
        self.present(returnView, animated: true, completion: nil)
    }
    @objc func Return(sender: UIButton!) {
        previosScreen()
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView ==  TipologiaUIPicker {
           return valori.count
        }
        else{
             return filteredDataMagazzini.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView ==  TipologiaUIPicker {
         CheckSelezione(row: row)
        return valori[row]
        }
        else{
             print("\n Nome Magazzino\(filteredDataMagazzini[row].Nome!)")
           return  filteredDataMagazzini[row].Nome
        }
    }


}
swift uipickerview
1个回答
0
投票

首先,您需要检查API调用中的结果,例如从服务器获取数据的方式。下一步是你需要将数据对象转换为MagazzinoStruct对象,然后你需要将该对象追加到你的filteredDataMagazzini数组中。

现在一旦你这样做,你需要在完成第一项任务后重新加载你的MagazzinoModUIPicker pickerview。

你需要在主队列中重新加载MagazzinoModUIPicker,因为你正在用asynchronous进行API调用。

© www.soinside.com 2019 - 2024. All rights reserved.