按用户搜索所有用户';firebase IOS中的s全名
我已经在我的项目中添加了按用户搜索所有用户';firebase IOS中的s全名,ios,swift,
elasticsearch,firebase,firebase-realtime-database,Ios,Swift,
elasticsearch,Firebase,Firebase Realtime Database,我已经在我的项目中添加了UISearchBar和UITableView。我需要根据ui搜索栏中的文本过滤所有用户,并显示在tableview中。我在firebase文档中对查询进行了研究,但没有找到。我只需要firebase查询部分字符串。有人能帮我吗 你好,希望这对你有帮助。。如果这个ans有效,那么请将其更正,以便其他用户可以很快识别答案 我希望我能理解你的问题 @IBOutlet var searchBar: UISearchBar! @IBOutlet var tblview: UIT
UISearchBar
和UITableView
。我需要根据ui搜索栏
中的文本过滤所有用户,并显示在tableview中。我在firebase文档中对查询进行了研究,但没有找到。我只需要firebase查询部分字符串。有人能帮我吗 你好,希望这对你有帮助。。如果这个ans有效,那么请将其更正,以便其他用户可以很快识别答案
我希望我能理解你的问题
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tblview: UITableView!
第1步:-制作arary
let data =
["xxx", "india, CA", "pakistan", "usa",
"china", "fgsfsgs", "San Diego, CA", "San Antonio, TX",
"Dallas, TX", "Detroit, MI"]
步骤2:-在viewDidLaod()方法上方声明此变量为全局变量
第三步:
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tblview.dataSource = self
searchBar.delegate = self
filteredData = data
}
步骤4:-使用Tableview委托方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
cell.textLabel?.text = filteredData[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
步骤5-使用以下方法
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
// When there is no text, filteredData is the same as the original data
if searchText.isEmpty {
filteredData = data //use your array in place od data
} else {
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredData = data.filter({(dataItem: String) -> Bool in
// If dataItem matches the searchText, return true to include it
if dataItem.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
return true
} else {
return false
}
})
}
self.tblview.reloadData()
}
你看过这个吗
我认为QueryToValue在这里很有用。我相信您的firebase数据库中有这些
- 名字
- 姓氏
- 名字
- 姓氏
- 全名
谢谢@akash的回答。此过滤器来自内存中的数据。但我需要直接从firebase数据库中筛选。您正在执行部分字符串搜索吗?e、 g.当用户在UISearchBar中键入时,您想从一个列表中自动填充,该列表会随着用户键入的内容越来越少而更新该列表?或者他们键入整个字符串并搜索整个字符串。嘿@Jay谢谢你的回复。实际上,我正在执行部分字符串搜索。Firebase不提供进行部分字符串搜索的方法(like或contains类型查询)。需要注意的是,如果要搜索字符串的第一部分,有一种技术可以使用.startingAt和.endingAt以及Unicode字符进行搜索,\uf8ff。此外,还可以设置Firebase结构来搜索部分字符串。我用你建议的方法解决了我的问题。我根据爱德华的建议重新构建了数据库。谢谢你的帮助,我真的很感激。@EdwardAnthony这将如何允许OP执行问题中所述的部分字符串搜索?@Jay如果是部分的,你可以自己创建索引。例如,字段
first1character
,first2character
,first3character
,等等。您可以使用queryOrderedByChild(“first\(input.characters.count)Character”)
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
// When there is no text, filteredData is the same as the original data
if searchText.isEmpty {
filteredData = data //use your array in place od data
} else {
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredData = data.filter({(dataItem: String) -> Bool in
// If dataItem matches the searchText, return true to include it
if dataItem.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
return true
} else {
return false
}
})
}
self.tblview.reloadData()
}
ref.child("users")
.queryOrderedByChild("fullName")
.queryEqualToValue("your full name")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
}