gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:43:52 25 4
gpt4 key购买 nike

我想从UIPickerView中的firestore检索数据。我创建了一个静态locationsArray。现在我想从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) -> ()){ 让 productsRef = db.collection("产品")

    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

}
}

最佳答案

将委托(delegate)和数据源分配给locationPickerView,即

   class yourVC : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { 

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

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

关于swift - 如何在 UIPickerView 中加载从 Firestore 检索的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49053128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com