如何在表视图中实现从Firestore搜索的UISearchBar?

麦克风

我想在我的应用程序中实现搜索功能。我希望当用户在搜索栏中搜索特定商店时,它将搜索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数据库实现此功能?

潘乔

好,这里是一些伪代码

为自己创建更易于使用的东西,以作为数据模型

   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
   }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在列表视图中搜索

如何实现从视图到表单的所有业务逻辑?

如何在表视图内的集合视图中更新数据?

如何在文本视图中实现水平尺?

如何在代码的网格视图中实现分页

如何在Cassandra实现视图中删除行?

SQL中的动态查询以及如何在视图中实现

如何在 Odoo 的树视图中实现导航面板?

如何在Web视图中实现进度指示器?

如何在Razor视图中实现<i>标记类的变量

在tabFragment视图中实现搜索栏

如何在屏幕之间实现从左向右滑动

如何在Django中实现从属下拉列表

有谁知道如何实现从表格视图单元格中的搜索栏中显示电话簿中的联系人列表?

如何在表视图中为行调用createEditor

如何在表视图中创建动态单元格?

如何在Android Eclipse的表视图中显示数据

如何在中心的表视图中添加文本标签?

如何在表视图中显示json模式

如何在每个视图上添加UISearchbar

用户单击“搜索”按钮后如何在视图中显示搜索结果?

如何将搜索友好数字的实现从命令式转换为功能式?

Flask:如何在不重写视图的情况下为每个视图实现通用的搜索栏?

如何在视图中相乘

如何在视图中心添加视图

如何在表视图标题视图中使用UISegmentedControl自动调整UIView的大小

如何在 HTML/JS/CSS 中的视图中实现导航效果?

如何在Svelte视图中最佳实现可共享数据模型

如何在 Angular 2+ 日历的周视图中实现点击事件?