ios - 使用 Firebase 在 UITableView 中使用搜索
问题描述
我有一个成功获取节点的UITableView
所有详细信息的地方。root->Data
我正在尝试实现 aSearchController
来过滤数据,但它似乎没有获取搜索结果并绑定到UITableView
.
我的 Firebase JSON 结构如下:
"Data" : {
"01" : {
"priority" : 2,
"search" : "school centre",
"title" : "School Centre"
},
这是来自的代码updateSearchResults
:
func updateSearchResults(for searchController: UISearchController) {
filterContent(searchText: self.searchController.searchBar.text!.lowercased())
}
func filterContent(searchText:String)
{
if (searchText.count == 0){
observePosts()
} else {
print(searchText)
observeSearchPosts(searchText: searchText)
}
}
observePosts
和之间的唯一区别observeSearchPosts
是数据库参考线:
func observeSearchPosts(searchText: String) {
let postsRef = Database.database().reference().child("Data").queryOrdered(byChild: "search").queryStarting(atValue: searchText)
print(postsRef)
postsRef.observe(.value, with: { snapshot in
var tempPosts = [Post]()
for child in snapshot.children{
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let title = dict["title"] as? String,
let logoImage = dict["image"] as? String,
let url = URL(string:logoImage),
let address = dict["address"] as? String,
let contact = dict["contact"] as? String,
let description = dict["description"] as? String{
let userProfile = UserProfile(title: title, photoURL: url)
let post = Post(id: childSnapshot.key, title: userProfile, description: description, image: userProfile, address: address, contact: contact)
print(post)
tempPosts.append(post)
}
}
self.posts = tempPosts
self.tableView.reloadData()
})
}
在observePosts
数据库中引用的是let postsRef = Database.database().reference().child("Data").queryOrdered(byChild: "priority")
.
解决方案
从firebase获取所有数据,然后在本地数组帖子中使用搜索......!然后重新加载tableview
推荐阅读
- ckeditor - CKEditor image2 改变初始大小
- javascript - 绑定功能阻止点击功能工作?
- spring - Spring dataRest插入如何仅传递id关系而不是实体
- r - R 中的 H2O 深度学习模型能否在保持多线程的同时可重现?
- cocoapods - 支持模拟器与 iOS 设备的不同供应商框架
- python - 如何确定最佳层数和激活函数
- javascript - 将值附加到 URL 搜索参数
- reactjs - 反应。如何在按钮和页面点击上隐藏结果
- python - 将 >2GB 数据传递给 tf.estimator
- python - 将 idxmin(axis=1) 与数据框一起使用时出现 TypeError