在 firebase 中按用户全名搜索所有用户 IOS
Search all user by user's full name in firebase IOS
我在我的项目中添加了 UISearchBar
和 UITableView
。我需要根据UISearchBar
中的文本过滤所有用户并显示在tableview中。我研究了 firebase 文档中的查询,但没有找到。我只需要部分字符串的 firebase 查询。谁能帮我?
您好,希望这对您有所帮助..如果这个答案有效,请使其正确,以便其他用户可以很快识别答案....
希望我理解你的问题.....
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tblview: UITableView!
第一步:-制作arary
let data =
["xxx", "india, CA", "pakistan", "usa",
"china", "fgsfsgs", "San Diego, CA", "San Antonio, TX",
"Dallas, TX", "Detroit, MI"]
step2:- 在 viewDidLaod() 方法上方声明这个全局变量
var filteredData: [String]!
第三步:-
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()
}
你看过这个 page 吗?
我认为 QueryToValue 在这里很有用。
我相信你的 firebase 数据库中有这些
- 名字
- 姓氏
而且firebase不会让你做多个where子句,为了解决这个问题,只需添加一个新字段,所以它会变成这样
- 名字
- 姓氏
- 全名
ref.child("users")
.queryOrderedByChild("fullName")
.queryEqualToValue("your full name")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
}
我在我的项目中添加了 UISearchBar
和 UITableView
。我需要根据UISearchBar
中的文本过滤所有用户并显示在tableview中。我研究了 firebase 文档中的查询,但没有找到。我只需要部分字符串的 firebase 查询。谁能帮我?
您好,希望这对您有所帮助..如果这个答案有效,请使其正确,以便其他用户可以很快识别答案....
希望我理解你的问题.....
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tblview: UITableView!
第一步:-制作arary
let data =
["xxx", "india, CA", "pakistan", "usa",
"china", "fgsfsgs", "San Diego, CA", "San Antonio, TX",
"Dallas, TX", "Detroit, MI"]
step2:- 在 viewDidLaod() 方法上方声明这个全局变量
var filteredData: [String]!
第三步:-
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()
}
你看过这个 page 吗?
我认为 QueryToValue 在这里很有用。
我相信你的 firebase 数据库中有这些
- 名字
- 姓氏
而且firebase不会让你做多个where子句,为了解决这个问题,只需添加一个新字段,所以它会变成这样
- 名字
- 姓氏
- 全名
ref.child("users")
.queryOrderedByChild("fullName")
.queryEqualToValue("your full name")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
}