swift - 基于 firebase 数据的负载选择器

标签 swift firebase firebase-realtime-database spinner

我正在尝试使用基于 firebase 字符串的矢量加载选择器。

这是我的变量:

var modeOfLearning = [String] ()
    let modeOfLearning1 = ["Select Mode:",
                          "In Person",
                          "Web-based",
                          "Simulation"]
    let modeOfLearning2 = ["Select Mode:",
                          "In Person",
                          "Web-based"]

在 viewDidLoad 中,我从 firebase 获取字符串的值,并使用 modeOfLearning1 或 2 加载 var modeOfLearning:

 override func viewDidLoad() {
        super.viewDidLoad()

let userEmail : String = (Auth.auth().currentUser?.email)!
        Database.database().reference().child("Student Profiles").queryOrdered(byChild: "email").queryEqual(toValue: userEmail).observe(.childAdded) { (snapshot) in

            let snapshotValue = snapshot.value as! Dictionary<String,String>

            let ustYear = snapshotValue["ustyear"]
            if ustYear == "First Year" {
                self.modeOfLearning = self.modeOfLearning1
} else if ustYear == "Second Year" {
                self.modeOfLearning = self.modeOfLearning2
}}

这是 pickerView 函数:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.modeOfLearning.count}

在 pickerView 函数中,modeOfLearning 的计数始终为零。这告诉我 pickerView 函数在 viewDidLoad 完成之前正在运行。我该怎么做才能让 pickerView func 等到 var modeOfLearning 加载完毕?

任何帮助将不胜感激!提前致谢!

最佳答案

而不是这样做 self.modeOfLearning = self.modeOfLearning1 尝试 self.modeOfLearning.append(self.modeOfLearning),并在完成处理程序中调用 pickerview.reloadAllComponents() 以触发选择器 View 函数。

关于swift - 基于 firebase 数据的负载选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49787753/

相关文章:

swift - 将 scanLocation 从 utf16 单位转换为 NSScanner (Swift) 中的字符索引

firebase - 如何在flutter中使用firebase的子添加事件检索列表?

javascript - 无法使用函数 :Shell 将新文档添加到 Firestore 中的集合

firebase-realtime-database - 我的应用程序的 Firebase 实时数据库与 Cloud Firestore

ios - 火力地堡, swift : `runTransactionBlock()` returns null

javascript - React - 按对象属性过滤

ios - 如何使用 swift 将相同的背景图像添加到我的 ios 应用程序中的所有 View ?

objective-c - 桥接头有什么用?我们应该避免使用桥接头吗?

ios - 如何在 Xcode 中为 tableView 更改原型(prototype)单元格的高度?

javascript - firebase firestore 从文档中获取对象/数组 - js