Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - Load data from json using UIPickerView

My Current View Controller is like this

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.
}


}

I don't know how to show value to UIPickerView from my json dictionary.i don't want key to show at UIPickerView.I am now stuck at "?????" statement.

This is my output for result

[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]

Please help!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you wanted to show data to a picker view, use declare 2 arrays as properties instead.

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

Inside your viewDidLoad function

var venues = JSON(jsonData!)

let d = venues.dictionaryValue

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

venuePicker.dataSource = self
venuePicker.reloadAllComponents()

Then inside 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
}

This way resultKeys[pickerView.selectedRowInComponent(0)] will return the key for the selected value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...