Ios 从firebase数据库筛选和显示搜索栏结果
我刚刚开始学习swift和firebase。我想添加一个搜索栏,允许用户搜索我的firebase数据库。这就是我想要的 我已经添加了搜索栏,我的问题是搜索结果的显示。 我创建了一个容器视图,其中包括名称、子描述和徽标,如上图所示,然后使用此函数进行设置Ios 从firebase数据库筛选和显示搜索栏结果,ios,swift,search,firebase-realtime-database,uisearchbar,Ios,Swift,Search,Firebase Realtime Database,Uisearchbar,我刚刚开始学习swift和firebase。我想添加一个搜索栏,允许用户搜索我的firebase数据库。这就是我想要的 我已经添加了搜索栏,我的问题是搜索结果的显示。 我创建了一个容器视图,其中包括名称、子描述和徽标,如上图所示,然后使用此函数进行设置 func searchResultContainer(){ searchResultView.addSubview(businesslogoView) searchResultView.addSubview(busines
func searchResultContainer(){
searchResultView.addSubview(businesslogoView)
searchResultView.addSubview(businessNameLabel)
searchResultView.addSubview(businessSectorLabel)
//need x. y, width, height constraints for searchResult
searchResultView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
searchResultView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
searchResultView.heightAnchor.constraint(equalToConstant: 220).isActive = true
}
然后,我将searchResult视图附加到var bussinesarray。然后将其插入到tableview中。请看下面我的代码
var businessArray = [NSDictionary]()
var filterBusiness = [NSDictionary]()
var ref : FIRDatabaseReference!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.insertRows(at: [IndexPath(row: self.businessArray.count-1, section: 0)], with: UITableViewRowAnimation.automatic)
ref.child("Businesses").queryOrdered(byChild: "Basic-Info/business").observe(.childAdded, with: { (snapshot) in
view.addSubview(searchResultView)
searchResultContainer()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// if searchbar is not empty "", then return filtered businesses if the user is not typing anything return all businesses.
if searchController.isActive && searchController.searchBar.text !=
""{
return filterBusiness.count
}
return self.businessArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let business : NSDictionary?
if searchController.isActive && searchController.searchBar.text !=
""{
business = filterBusiness[indexPath.row]
}
else
{
business = self.businessArray[indexPath.row]
}
cell.textLabel?.text = business?["Business"] as? String
cell.detailTextLabel?.text = business?["handle"] as? String
return cell
}
func filterContent (searchText:String) {
self.filterBusiness = self.businessArray.filter{ Businesses in
let businessName = Businesses["Business"] as? String
return(businessName?.contains(searchText.lowercased()))!
}
tableView.reloadData()
}
func updateSearchResults(for searchController: UISearchController) {
// update the search results
filterContent(searchText: self.searchController.searchBar.text!)
}
我没有从firebase DB获得搜索结果,如何正确实现firebase DB的搜索结果?我正在以编程方式构建所有内容,请提供一个示例代码,非常感谢 本教程对我了解类似的实现有很大帮助,请参见本教程底部的代码 本教程之外的代码调整包括以下代码。我仍然可以对if/else部分进行一些清理,但是对我来说,两个关键的概念是使用模型并通过以下方式获得正确的目标:let temp:NSString=text.EntityName!作为NSString 模型文件: 视图控制器调整
过滤内容被称为?我认为您没有将请求发送到fireBase DB,您只是在筛选您拥有的数组。您将如何将请求发送到fireBase DB查看文档,查看startAt()、endAt()和equalTo()
class Dealer: NSObject{
var DealerNumber: String?
var EntityName: String?
//matchup all other firebase data fields
}
var dealerList = [Dealer]()
var filterDealers = [Dealer]()
---
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filterDealers = dealerList.filter({ (text) -> Bool in
let temp: NSString = text.EntityName! as NSString
let range = temp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(filterDealers.count == 0){
searchActive = false;
} else {
searchActive = true;
}
refreshTable()
}
----
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
if(searchActive){
cell?.textLabel?.text = filterDealers[indexPath.row].EntityName
cell?.detailTextLabel?.text = filterDealers[indexPath.row].DealerNumber
} else {
cell?.textLabel?.text = dealerList[indexPath.row].EntityName
cell?.detailTextLabel?.text = dealerList[indexPath.row].DealerNumber
}
return cell!;
}