gpt4 book ai didi

swift - 使用 uipickerView 时将 OF 对象保存到数组中

转载 作者:行者123 更新时间:2023-11-28 06:54:24 26 4
gpt4 key购买 nike

我对 Swift 比较陌生。我在 pickerView 中使用 PFQuery 时遇到了问题。

我正在尝试实现一个 2 组件 pickerView,如下所示:

组件 0:组件 1

“A”:“Altivo”、“Altrez”

“B”:“富康”、“比茹”

从组件 0 中选择“A”将在组件 1 中显示“Altivo”、“Altrez”

使用组件 0 的值从 Parse 查询组件 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 中。

谁知道为什么?

在此先感谢您的帮助。

最佳答案

我设法通过使用 didset{} 方法从另一个线程解决了这个问题。

var condoNameList: [String] = [] {
didSet {
self.condoNameListTemp = condoNameList
condoName.inputView = condoPicker
condoPicker.reloadAllComponents()
}
}

关于swift - 使用 uipickerView 时将 OF 对象保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34109067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com