如何在 UIPickerView 中加载从 Firestore 检索到的数据?

Posted

技术标签:

【中文标题】如何在 UIPickerView 中加载从 Firestore 检索到的数据?【英文标题】:How to load data retrieved from Firestore in UIPickerView? 【发布时间】:2018-03-01 15:31:14 【问题描述】:

我想从 UIPickerView 中的 firestore 中检索数据。我创建了一个静态 locationsArray。现在我想从 pickerView 中的 firestore 中检索位置数据。

var db:Firestore! let locationsArray = ["Mirpur", "Gulshan", "Dhanmondi", "Mohammadpur"]

func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 

    return locationsArray.count

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 

    if pickerView == locationPickerView
        location.text = locationsArray[row]
    


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
     return locationsArray[row]
 

func fetchProducts(completion: @escaping (Bool) -> ()) 让 productsRef = db.collection("Products")

    var products = [Product]()

     if let loc = location.text, let nop = numberOfPeople.text, let datesc = dateSchedule.text, let startingPrice = PriceFrom.text, let endingPrice = PriceTo.text 

        productsRef.whereField("location", isEqualTo: loc).whereField("numberOfPeople", isEqualTo: nop).whereField("offerPrice", isGreaterThanOrEqualTo: startingPrice).whereField("offerPrice", isLessThanOrEqualTo: endingPrice)
            .addSnapshotListener (querySnapshot, err) in
                if let err = err 
                    print("Error getting documents: \(err)")
                 else 
                    var productDictionary = [[String: Any]]()

                    for document in querySnapshot!.documents 
                        productDictionary.append(document.data())
                        //print("\(document.documentID) => \(document.data())")
                    
                    let checkingDate = datesc
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "MM-dd-yyyy"

                    guard let userDate = dateFormatter.date(from: checkingDate) else return

                    for (index,dictionary) in productDictionary.enumerated().reversed()
                        guard let startDate = dictionary["starDate"] as? String else return
                        guard let endDate = dictionary["endDate"] as? String else return

                        guard let startDateObject = dateFormatter.date(from: startDate) else return
                        guard let endDateObject = dateFormatter.date(from: endDate) else return

                        let isWithinDates = userDate.isBetween(startDateObject, and: endDateObject)

                        if isWithinDates
                            //Do nothing
                        else
                            productDictionary.remove(at: index)

                            let product = Product(dictionary: dictionary)
                            products.append(product)

                        
                    
                

                self.productsArray = products

                completion(true)

           // end of snapshot listener

        

【问题讨论】:

【参考方案1】:

将委托和数据源分配给 locationPickerView 即

   class yourVC : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate  

      override func viewDidLoad() 
            super.viewDidLoad()
            locationPickerView.delegate = self
            locationPickerView.datasource = self
    

试试这可能对你有用。获取数据后添加这一行self. locationPickerView.reloadComponent(1)

【讨论】:

我不明白,请求完成后在哪里重新加载locationPickerView。

以上是关于如何在 UIPickerView 中加载从 Firestore 检索到的数据?的主要内容,如果未能解决你的问题,请参考以下文章

数据未在 JQGrid 中加载

UIPickerView 在 IOS 8 中不起作用

如何在字符串中加入回车换行,tab字符

知道 UICollectionView 是不是正在滚动

这是 iOS 的哪个 UI 元素?

在 UIViewController 中加载和卸载数据