【发布时间】:2020-11-05 21:28:25
【问题描述】:
我想在我的应用中实现搜索功能。我希望当用户在搜索栏中搜索特定商店时,它会搜索 firestore 数据库并使用他输入的特定商店填充 tableview。
但我被困在 searchBar 功能以及如何查询 firestore 以检索和存储用户在搜索栏中输入的文本。
到目前为止,这是我的代码:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredShops = []
db.collection("Shops").whereField("name", isLessThanOrEqualTo: searchText).addSnapshotListener{ (query, error) in
if error != nil {return}
guard let doucments = query?.documents else {return}
for doc in doucments {
self.sName = doc["name"] as? String
self.sLoc = doc["location"] as? String
self.sImg = doc["ShopHeaderImg"] as? String
self.filteredShops.append(self.sName!)
self.filteredShops.append(self.sLoc!)
self.filteredShops.append(self.sImg!)
}
}
print("is typing")
}
}
extension SearchTableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredShops.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = searchTV.dequeueReusableCell(withIdentifier: "searchShop")! as UITableViewCell
cell.textLabel?.text = filteredShops[0]
return cell
}
}
我怎样才能用 firestore 数据库来实现呢?
【问题讨论】:
-
你是问如何查询数据库,或者如何显示结果?
-
@Pancho 请查看我编辑的问题,我在问如何查询数据库并以正确的方式显示结果。我展示的上述方法对我不起作用。
标签: ios swift firebase google-cloud-firestore