2016-03-03 29 views
-1

Im新到swift。在Swift中使用JSON數組填充UIViewPicker

我正在查詢Web服務以獲取相關數據。

[ 
    { 
    "id" : "1", 
    "description" : "9 Sea View Road" 
    }, 
    { 
    "id" : "2", 
    "description" : "10 Random Street" 
    } 
] 

如何篩選此返回的數據,然後填充UIPickerView?

Ive得到了與此有關的數據:

let venues = JSON(data:tabletData) 

感謝

回答

0
//First use to viewdidload 
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) { 
    let venues = try! NSJSONSerialization.JSONObjectWithData(data, options: []) 
} 


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

// The number of rows of data 
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return venues.count 
} 

// The data to return for the row and component (column) that's being passed in 
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return venues[row].valueForKey("description")as? String 
} 

我希望這項工作。