2017-02-04 1 views
0

PickerView를 사용하여 JSON 배열의 데이터를 표시하고 있습니다.pickerview에서 선택된 행의 다른 값 얻기

JSON 어레이는 여러 개의 키를 가지며, I는 pickerview 채우는 키의 두 가지의 값을 통합하고 :

: 유저가 행을 선택

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

나는 다음과 같이 해결이 :

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 말했다, 나는 사전의 배열을 변경하고 지금은 잘 작동합니다.

관련 문제