您如何使用已放入数组中的 Firebase 值来填充选择器。这是我用来向数组添加值并打印它们的代码:
let dbRef1 = FIRDatabase.database().reference().child("Live Scoring Male")
dbRef1.observe(.childAdded, with: { snapshot in
let data = snapshot.value as? [String:Any] ?? [:]
let name = data["name"] as? String ?? ""
self.compArray.append(name)
print("\(name)")
})
然而,当我调用时,数组值没有被加载到选择器中:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 1) {
return compArray[row]
}
}
如果我对数组中的字符串进行硬编码,则此代码有效,但如果我尝试从 Firebase 加载值,则无效。
最佳答案
您需要重新加载 pickerView
组件以使用新的数据源数组更改它。
let dbRef1 = FIRDatabase.database().reference().child("Live Scoring Male")
dbRef1.observe(.childAdded, with: { snapshot in
if let dic = snapshot.value as? [String:Any], let name = dic["name"] as? String {
self.compArray.append(name)
print("\(name)")
//Reload the pickerView components
self.pickerView.reloadAllComponents()
//Or you can reload the pickerView's specific component
self.pickerView.reloadComponent(0)
}
})
关于arrays - firebase 子值作为数组中的字符串来填充选择器 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42153742/