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

时间:2018-03-01 15:31:14

标签: swift firebase google-cloud-firestore

我想从UIPickerView.i中的firestore重新获取数据是创建一个静态locationsArray.Now我想从pickerView中的firestore中重新获取位置数据。

var db:Firestore! 让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(完成:@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

        }
}

1 个答案:

答案 0 :(得分:0)

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

   class yourVC : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { 

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

试试这个可能适合你。获取数据后,添加此行self. locationPickerView.reloadComponent(1)