【发布时间】:2015-12-05 18:10:49
【问题描述】:
我对 Swift 比较陌生。我在 pickerView 中使用 PFQuery 时遇到了问题。
我正在尝试实现一个 2 组件的 pickerView,如下所示:
组件 0:组件 1
“A”:“Altivo”、“Altrez”
“B”:“贝弗利”、“比茹”
从组件 0 中选择“A”将在组件 1 中显示“Altivo”、“Altrez”
使用来自 Component 0 的值从 Parse 中查询来自 Component 1 的值
这是我的代码:
var condoNameList: [String] = [String]()
var condoPicker = UIPickerView()
var condoAlphabet: [String] = [String]()
private let prefixComponent = 0
private let condoNameComponent = 1
override func viewDidLoad() {
super.viewDidLoad()
condoAlphabet = ["A", "B"]
}
//Start: For condoName picker
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == prefixComponent{
return condoAlphabet.count
}else {
return self.condoNameList.count
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == prefixComponent {
condoNameList.removeAll()
let query = PFQuery(className: "condodirectory")
let selectedalphabet: String = condoAlphabet[row] as String!
query.whereKey("condoName", hasPrefix: selectedalphabet)
query.findObjectsInBackgroundWithBlock{
(objects:[PFObject]?, error:NSError?)->Void in
if error == nil{
for object in objects!{
//print(object)
let condoName:String = object["condoName"] as! String
self.condoNameList.append(condoName)
}
}
//print("condo Name List is \(self.condoNameList)") //Position 1
}
print("condo Name List is \(self.condoNameList)") //Position 2
condoPicker.reloadComponent(condoNameComponent)
condoPicker.selectRow(0, inComponent: condoNameComponent, animated: true)
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == prefixComponent{
return condoAlphabet[row]
}else {
return self.condoNameList[row]
}
}
问题出在位置 1,打印函数能够打印出 condoNameList,但在位置 2,它会打印一个空数组。因此,组件 1 的列表没有显示在 PickerView 中。
有人知道为什么吗?
提前感谢您的帮助。
【问题讨论】:
-
您的打印位置 2 在块之外。在这种情况下,打印发生在查询之前。 de block 中的所有内容都发生在查询之后