用 json GET 创建的先前数组填充 UiPickerView(我得到“?”而不是数据)
Posted
技术标签:
【中文标题】用 json GET 创建的先前数组填充 UiPickerView(我得到“?”而不是数据)【英文标题】:fill the UiPickerView with a previous array created with json GET ( i get '?' instead the data) 【发布时间】:2015-02-18 15:53:00 【问题描述】:当我尝试在 UiPickerView 中显示数组时遇到问题,我在控制台中检查数组数据(来自 json),数据在那里,但是当我尝试填充选择器时,我得到了'? ' (数组的每一行 1 个)
这是我的代码
import UIKit
类 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 pickerView(pickerView: UIPickerView, didSelectRow row: Int, forComponet component: Int) println("la desc/es (self. pickerData[row])") " 但我不知道它是否不起作用,因为我无法使 uipickerview 正常工作或在某些时候出错 【参考方案1】:委托和数据源函数有一些问题,这是使用它们的正确方法
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)
【讨论】:
以上是关于用 json GET 创建的先前数组填充 UiPickerView(我得到“?”而不是数据)的主要内容,如果未能解决你的问题,请参考以下文章