用之前使用 json GET 创建的数组填充 UiPickerView(我得到的是“?”而不是数据)
fill the UiPickerView with a previous array created with json GET ( i get '?' instead the data)
当我尝试在 UiPickerView 中显示数组时遇到问题,我在控制台中检查了数组数据(来自 json)数据在那里,但是当我尝试填充时我得到的选择器“?” (数组的每一行 1)
这是我的代码
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var NameLabel: UILabel!
@IBOutlet var myPicker: UIPickerView!
var pickerData = [String]()
@IBAction func Butonio(sender: AnyObject) {
var urlPath: String = "link with json encode response"
//self.NameLabel.text = urlPath;
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var err: NSError?
// println(data)
if let json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:
&err) as? NSArray{
if let descripcion = json[0]["descripcion"] as? NSString {
let descripcion2 = NSUUID().UUIDString
var descripcion3 = UIDevice.currentDevice().identifierForVendor.UUIDString
let stringo : Int = json.count
println( "el nmuero es \(stringo)")
var stringc = ""
// NSArray colors = [[Int:String]]()
//var pickerData = [String]()
var j = 0
while j == 0{
if self.pickerData.isEmpty{
j=1
} else {
self.pickerData.removeLast()
}
}
for i in 0..<stringo{
if let descr = json[i]["descripcion"] as? NSString{
//println( "la desc es \(descr)")
//colors[i] = descr
//var colors: [Int:String] = [i : descr]
self.pickerData.append(descr)
stringc += self.pickerData[i]+" , "
println( "la desc/ es \(self.pickerData[i])")
}
}
self.myPicker.dataSource = self
self.myPicker.delegate = self
self.myPicker.reloadAllComponents()
println( "la desc/total es \(stringc)")
return
}
}
})
}
//MARK: - Delegates and data sources
//MARK Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.pickerData.count
}
//MARK Delegate
func pickerView(pickerView: UIPickerView, tittleForRow row: Int, forComponet component: Int) -> String!{
//println(row)
return self.pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, forComponet component: Int) {
//println(row)
println( "la desc/ es \(self.pickerData[row])")
// self.NameLabel.text = self.pickerData[row]
}
}
委托和数据源函数有一些问题,这是使用它们的正确方法
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
NameLabel.text = pickerData[row]
self.view.endEditing(true)
}
当我尝试在 UiPickerView 中显示数组时遇到问题,我在控制台中检查了数组数据(来自 json)数据在那里,但是当我尝试填充时我得到的选择器“?” (数组的每一行 1)
这是我的代码
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var NameLabel: UILabel!
@IBOutlet var myPicker: UIPickerView!
var pickerData = [String]()
@IBAction func Butonio(sender: AnyObject) {
var urlPath: String = "link with json encode response"
//self.NameLabel.text = urlPath;
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var err: NSError?
// println(data)
if let json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:
&err) as? NSArray{
if let descripcion = json[0]["descripcion"] as? NSString {
let descripcion2 = NSUUID().UUIDString
var descripcion3 = UIDevice.currentDevice().identifierForVendor.UUIDString
let stringo : Int = json.count
println( "el nmuero es \(stringo)")
var stringc = ""
// NSArray colors = [[Int:String]]()
//var pickerData = [String]()
var j = 0
while j == 0{
if self.pickerData.isEmpty{
j=1
} else {
self.pickerData.removeLast()
}
}
for i in 0..<stringo{
if let descr = json[i]["descripcion"] as? NSString{
//println( "la desc es \(descr)")
//colors[i] = descr
//var colors: [Int:String] = [i : descr]
self.pickerData.append(descr)
stringc += self.pickerData[i]+" , "
println( "la desc/ es \(self.pickerData[i])")
}
}
self.myPicker.dataSource = self
self.myPicker.delegate = self
self.myPicker.reloadAllComponents()
println( "la desc/total es \(stringc)")
return
}
}
})
}
//MARK: - Delegates and data sources
//MARK Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.pickerData.count
}
//MARK Delegate
func pickerView(pickerView: UIPickerView, tittleForRow row: Int, forComponet component: Int) -> String!{
//println(row)
return self.pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, forComponet component: Int) {
//println(row)
println( "la desc/ es \(self.pickerData[row])")
// self.NameLabel.text = self.pickerData[row]
}
}
委托和数据源函数有一些问题,这是使用它们的正确方法
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
NameLabel.text = pickerData[row]
self.view.endEditing(true)
}