我对Swift比较新。我在pickerView中使用PFQuery时遇到了一个问题。
我正在尝试实现一个2组件pickerView,如下所示:
组件0:组件1
“A”:“Altivo”,“Altrez”
“B”:“Beverly”,“Bijou”
从组件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中。
任何人都知道为什么?
提前感谢您的帮助。
答案 0 :(得分:1)
我设法通过使用didset {}方法从另一个线程解决问题。
python C:\Python27\Projects\hello.p