arrays - firebase 子值作为数组中的字符串来填充选择器 swift 3

标签 arrays swift string firebase firebase-realtime-database

您如何使用已放入数组中的 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/

相关文章:

Ruby 创建数组形式的 git 日志?

php - 如何检查多个特定字符的字符串?

c++ - 将 Strncmp 与文件中的字符串一起使用

c - 一维数组中的矩阵转置

c++ - 我可以安全地复制 vector<array> 吗?

java - 检查基元数组是否包含另一个数组中的所有基元的最佳方法

ios - Admob Interstitial 时间限制 iOS Swift

iOS黑暗模式。用户界面样式在进入后台时来回更改

ios - 隐藏呈现的 View 导致所有 View 隐藏 Swift

python - Python 的 SequenceMatcher 是如何工作的?