使用UIPickerView从json加载数据

时间:2015-01-27 09:22:58

标签: json swift uipickerview alamofire

我的当前视图控制器就像这样

import UIKit
import Alamofire

class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource{

@IBOutlet var venuePicker : UIPickerView?

var result = [String:String]()

var resultArray = [String]()

override func viewDidLoad() {

    self.venuePicker?.delegate = self

    Alamofire.request(.POST, "http://example.com/xxx/xx/xx").responseJSON() {
        (request, response, jsonData, error) in

        var venues = JSON(jsonData!)

        let d = venues.dictionaryValue

        for (k, v) in venues {
            self.result[k] = v.arrayValue[0].stringValue
        }
        self.resultArray = self.result.values.array
        self.venuePicker?.reloadAllComponents()
    }

}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return resultArray.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return resultArray[row]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

我不知道如何从我的json字典中显示UIPickerView的价值。我不想在UIPickerView上显示关键字。我现在卡在" ?????&# 34;言。

这是我的结果输出

[C2517: ORIX Kobe Nyusatsu, C2510: NPS Sendai Nyusatsu, C2033: JU Gunma, C2053: KAA Kyoto, C2035: JU Ibaraki, C2077: USS Gunma, C2024: ISUZU Kobe, C2505: NAA Osaka Nyusatsu, C2529: SMAP Sapporo Nyusatsu, C2502: L-Up PKobeNyusatsu, C2005: ARAI Sendai, C2072: TAA Minami Kyushu]

请帮助!!!

2 个答案:

答案 0 :(得分:1)

如果要将数据显示到选择器视图,请使用声明2个数组作为属性。

var resultKeys = [String]()
var resultValues = [String]()

viewDidLoad功能

var venues = JSON(jsonData!)

let d = venues.dictionaryValue

for (k, v) in venues {
    resultKeys.append(k)
    resultValues.append(v)
}

venuePicker.dataSource = self
venuePicker.reloadAllComponents()

然后在titleForRow

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return resultValues[row]
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return resultValues.count
}

这样resultKeys[pickerView.selectedRowInComponent(0)]将返回所选值的键。

答案 1 :(得分:0)

Alamofire完全异步。这意味着当数据准备好时将执行块代码。到那时,PickerView已经加载,并带有一个空数组。这就是PickerView为空的原因。因此,为了显示我们想要的数据,我们需要强制PickerView重新加载:

override func viewDidLoad() {
    self.venuePicker?.delegate = self // This should be outside the block, my bad.
    //...
    Alamofire.request(...) {in
        //...
        venuePicker?.reloadAllComponents() // hopefully this will work
    } 

如果您不熟悉异步编程的概念,您也可以选择使用同步方式。使用我写的https://github.com/skyline75489/swift-playground/blob/master/swift-playground%2FRequests.swift代码,我们可以轻松发送同步请求。

if let jsonData = Requests.get("YOUR_URL") {
     let venues = JSON(jsonData)
     //...
}

我们之所以有异步,有时候数据可能会很大并且使用同步方式会导致viewDidLoad永远加载。这对用户体验来说太糟糕了。但是,如果数据很小。同步方式可以足够快地完成任务,用户不会感觉到它。使用同步方式完全没问题。