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



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

var db:火堆!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( -> ((({ let 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
        }
}

将委托和数据源分配给位置PickerView,即

   class yourVC : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { 
      override func viewDidLoad() {
            super.viewDidLoad()
            locationPickerView.delegate = self
            locationPickerView.datasource = self
    }
}

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

最新更新