【问题标题】:How to Implement UISearchBar searching from firestore in table view?如何在表格视图中实现从 firestore 搜索 UISearchBar?
【发布时间】: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


【解决方案1】:

这里有一些伪代码

为自己创建一些更易于使用的东西,作为数据模型为您服务

   struct Shop {
       let name: String?
       let location: String?
       let image: String?
  
       init(from document: Document) {
           name = document["name"] as? String
           location = document["location"] as? String
           image = document["ShopHeaderImg"] as? String
       }
   }

然后在你的视图控制器中添加数据源

var shops: [Shop] = []
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    db.collection("Shops")
      .whereField("name", contains: searchText)
      .addSnapshotListener { [weak self] (query, error) in
        // make sure you capture self weak as it may lead to memory leak
        guard let self = self, let documents = query?.documents else { return }           
        // simply transform your Documents to Shops and update your dataSource
        self.shops = documents.map { Shop(from: $0) }
        // Reload your table view and show the result
        self.tableView.reloadData()
    }
  }
}

extension SearchTableViewController: UITableViewDataSource {

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return shops.count
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = searchTV.dequeueReusableCell(withIdentifier: "searchShop")! as UITableViewCell
       let shop = shops[indexPath.row]
       cell.textLabel?.text = shop.name
       return cell
   }
}

【讨论】:

  • 惊人的答案,它现在有效。有没有办法让它以前缀开头?所以如果用户开始输入“St”,它会在开头显示所有带有前缀“St”的商店?
  • 查看有关如何查询它的文档。我对firestore不太熟悉
猜你喜欢
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多