2017-02-04 3 views
0

PickerViewを使用してJSON配列のデータを表示しています。pickerviewで選択した行から別の値を取得する

JSONアレイは複数のキーを有しており、Iはpickerviewを移入するためのキーの2の値をマージしています:

let fullName = (friendObj["nombre"] as! String) + " " + (friendObj["apellidos"] as! String) 

self.searchResults.append(fullName) 

ユーザが行を選択すると、選択された値が文字列のfullNameある:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return searchResults.count 
    } 
    //MARK: Delegates 
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return searchResults[row] 
    } 

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     print (searchResults[row]) 

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

文字列fullNameを行タイトルとして表示するにはどうすれば結果として別のJSONキーの値を取得できますか?

do { 

        // Convert data returned from server to NSDictionary 
        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary 

        // Cleare old search data and reload table 
        self.searchResults.removeAll(keepingCapacity: false) 


        // If friends array is not empty, populate searchResults array 
        if let parseJSON = json { 

         if let friends = parseJSON["friends"] as? [AnyObject] 
         { 
          for friendObj in friends 
          { 
           let fullName = (friendObj["nombre"] as! String) + " " + (friendObj["apellidos"] as! String) 

           self.searchResults.append(fullName) 


          } 


          self.pickerAsesores.reloadAllComponents() 

         } else if(parseJSON["message"] != nil) 
         { 
          // if no friends returned, display message returned from server side 
          let errorMessage = parseJSON["message"] as? String 

          if(errorMessage != nil) 
          { 
           // display an alert message 
           self.displayAlertMessage(errorMessage!) 
          } 
         } 
        } 
+1

各行に表示する文字列の配列を格納するのではなく、JSONオブジェクトを表すオブジェクトの配列を格納してから、 'cellForRow:at'に関連する文字列を取得して行を読み込む必要があります。 – Paulw11

+0

@ Paulw11、私はオブジェクトの配列に文字列の配列を変換する方法を探しています – mvasco

+1

文字列はすでにオブジェクトですが、私は元のJSON情報を格納することです。あなたはJSONからオブジェクトを取得する方法を示していませんが、辞書があるように見えます。配列の中に何らかの 'friendObj'を格納してください。 – Paulw11

答えて

0

を次のように私はそれを解決しています:EDITED

for friendObj in friends 
        { let fullName = ["Nombre": friendObj["nombre"] as! String, "Apellidos": friendObj["apellidos"] as! String, "Id": friendObj["id"] as! String, "Tel": friendObj["tel"] as! String, "Email": friendObj["email"] as! String] 

         self.searchResults.append(fullName) 

           } 
... 

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     print (searchResults[row]["Id"]!) 
     let prefs = UserDefaults.standard 
     prefs.setValue(searchResults[row]["Id"], forKey: "miAsesorId") 
     prefs.setValue(searchResults[row]["Nombre"]! + " " + searchResults[row]["Apellidos"]!, forKey: "miAsesor") 
     prefs.setValue(searchResults[row]["Tel"], forKey: "miAsesorTel") 
     prefs.setValue(searchResults[row]["Email"], forKey: "miAsesorEmail") 

    } 

を@ Paulw11で述べたように、私は辞書のための配列を変更し、それが今で正常に動作します。

関連する問題