我这里有2个选择器,图片的值被分配给从firebase中提取的数据。它被分配给一个数组,然后将这些值分配给选择器。这不起作用,我不确定为什么。 这是我的代码:
import UIKit
import Firebase
import FirebaseDatabase
class SearchSelectVC: UIViewController, UIPickerViewDelegate , UIPickerViewDataSource{
@IBOutlet weak var locationPicker: UIPickerView!
@IBOutlet weak var datePicker: UIPickerView!
@IBOutlet weak var locationLB: UILabel!
@IBOutlet weak var dateLB: UILabel!
var locData: [String] = [String]()
var dateData : [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
initPickers()
getLoc(onCompletion: { (data) in
print(data)
self.locData = (data)
print(self.locData)
})
}
func getDate(onCompletion: @escaping ([String]) -> ()) {
var data : [String] = []
let selectedLoc = locationLB.text
var dateRef: DatabaseReference!
dateRef = Database.database().reference().child("launch").child((selectedLoc)!)
dateRef.observe(.value) { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("date = \(key)")
//print(data)
}
onCompletion(data)
}
}
func getLoc(onCompletion: @escaping ([String]) -> Void){
print("here3")
var data : [String] = []
var locRef: DatabaseReference!
locRef = Database.database().reference()
print("here4")
locRef.child("launch").observe(.value) { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("location = \(key)")
//print(data)
}
onCompletion(data)
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if(pickerView == locationPicker){
return 1
}
else if(pickerView == datePicker){
return 1
}
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView == locationPicker) {
return locData.count
}
else if (pickerView == datePicker) {
return dateData.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == locationPicker) {
return locData[row]
}
else if(pickerView == datePicker){
return dateData[row]
}
return "thisisbad"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if(pickerView == locationPicker){
updateLabelLoc()
getDate(onCompletion: { (data) in
print(data)
self.dateData = data
})
}
else if(pickerView == datePicker){
updateLabelDate()
}
}
func updateLabelLoc(){
let location = locData[locationPicker.selectedRow(inComponent: 0)]
locationLB.text = location
}
func updateLabelDate(){
let date = dateData[datePicker.selectedRow(inComponent: 0)]
dateLB.text = date
}
func initPickers(){
locationPicker.delegate = self
locationPicker.dataSource = self
datePicker.delegate = self
datePicker.dataSource = self
}
}
数据位于完成处理程序中,完成后,它会正确返回数据库中的数据。拣货员在其中没有任何数据显示。 谢谢!
答案 0 :(得分:0)
创建:
func updatePickers() {
locationPicker.reloadAllComponents()
datePicker.reloadAllComponents()
}
在print(self.locData)
之后(在getLoc(onCompletion:)
中(在viewDidLoad()中),放置:
self.updatePickers()